hyperboreean
hyperboreean

Reputation: 8333

Makefile linking issues

I wrote the Makefile for an easier way to run the unit tests (the default target 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.

Upvotes: 0

Views: 155

Answers (3)

luis
luis

Reputation: 81

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

Upvotes: 4

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?).

Upvotes: 2

thiton
thiton

Reputation: 36049

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.

Upvotes: 0

Related Questions