简体   繁体   中英

Can't compile with Makefile from different source files in different directories

I've got a C++ project with this directory structure:

src
├── database
│   ├── database.hpp
│   ├── leveldb.cpp
│   └── leveldb.hpp
├── Makefile
└── server.cpp

server.cpp includes "database/database.hpp" and "database/leveldb.hpp". leveldb.cpp includes "leveldb.hpp"

my Makefile is this so far (keep in mind that $LEVELDB is the path to the Google's leveldb libraries and is different from my leveldb.hpp and cpp files, which represent a custom made class):

CC=g++
LEVELDB=/route/to/Googles/library/leveldb
BOOST=/route/to/boost/cpp-netlib-0.9.2

DATABASE=/route/to/my/project/src/database

CFLAGS=-I$(LEVELDB)/include -I$(BOOST) -Wall -O2
LDFLAGS=-L$(LEVELDB) -lleveldb -lpthread -lboost_system -lboost_thread -pthread

all:server

server: server.cpp database/leveldb.cpp
    $(CC) -o $@ $(CFLAGS) -I$(DATABASE) $< $(LDFLAGS) -L$(DATABASE)

I can't get it to compile. It gives me an error "undefined reference to `LevelDB::LevelDB(std::basic_string, std::allocator >)'" which I know is a compilation error because of the Makefile, because everything worked fine when I had it all in one single source file.

I'd appreciate so much your help.

Thanks :)

You haven't told us whether you are trying to make (or make all or make server ) or make servidor . Either way, the problem is that you are not linking the leveldb definitions into your target.

In this rule:

servidor: server.cpp database/leveldb.cpp
    $(CC) -o $@ $(CFLAGS) -I$(DATABASE) $< $(LDFLAGS) -L$(DATABASE)

You are using the automatic variable $< , which means the first prerequisite, ( server.cpp ). You should use $^ , which means all prerequisites ( server.cpp database/leveldb.cpp ).

If you make or make all or make server , Make will attempt to build server from server.cpp using default rules which make no mention of leveldb.cpp . Are you sure you don't want the rule to be "all:servidor"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM