Makefile linking issues
I wrote the Makefile for an easier way to run the unit tests (the default targ开发者_运维知识库et is test). The directories structure is as follows:
lib
- google c++ test framework
src
- source files
test
- unit tests
In src
I have the files a.h
, a.cc
, b.cc
(which holds main) and I want to test class A()
which is defined in a.h
. All in all, because the order of linking is:
g++ -Wall -c -o obj/src/a.o src/a.cc
g++ -Wall -c -o obj/src/b.o src/b.cc
g++ -o all obj/src/a.o
I get the error:
(.text+0x18): undefined reference to main'
collect2: ld returned 1 exit status
make: *** [all] Error
even though I don't actually need a main()
defined in src
since I already have a main()
defined in test
which runs the unit tests.
I don't understand very well what you want to do, but if your main is in, say, test.o, you need to specify that in you linking line. For example:
g++ -o all obj/src/a.o test.o
When you say
g++ -o all obj/src/a.o
g++
thinks you've asked it to build a executable, and accordingly is looking for the entry point. Presumably you meant to ask it for something else (a library, maybe?).
Even though it doesn't solve your current problem, it might be helpful to know that you are re-engineering automake
. You shouldn't be writing Makefiles for C or C++ projects by hand nowadays.
精彩评论