Makefile creating several mains as unit testing C++ -


i quite new makefiles want create c++ project unit tests.

my project directory looks this:

makefile src/     main.cpp     file1.cpp     file1.h     file2.cpp     file2.h     ... test/     file1_test.cpp     file2_test.cpp     ... 

i want makefile compile program in src/ , compile programs in test/ directory. each *_test.cpp file contains main() function. have tried lot of things every time try compile run new problem , feels should quite easy task.

(btw, got inspired tutorial http://c.learncodethehardway.org/book/ex28.html)

thanks help!

edit:

my current makefile looks this. (i had make changes fit more general example)

cflags = libs = ldflags = -l/usr/local/ cc = g++  sources=$(wildcard src/**/*.cpp src/*.cpp) objects=$(patsubst %.cpp,%.o,$(sources))  target=build/myprogram  test_src=$(wildcard test/*_test.cpp) tests=$(patsubst %.cpp,%.o,$(test_src)) tests_prg=$(patsubst %.cpp,%,$(test_src)) tests_obj=$(patsubst test/%_test.cpp,src/%.o,$(test_src))  all: $(target)   $(target): build $(objects)         g++ -o $(objects) $(cflas) $(libs)  build:         @mkdir -p build         @mkdir -p bin  .phony: tests tests: $(tests_obj)                                                                                                                                                 g++ $(tests_obj) $(cflags) -o $(tests_prg)                                                                                                           clean:         rm -rf build $(objects)                                                                                                                                     rm -f tests/tests.log         find . -name "*.gc*" -exec rm {} \;         rm -rf `find . -name "*.dsym" -print` 

i ran makefile as-is, after fixing whitespace errors on place.

however, if that, not behaviour claim: error g++ telling me second file1_test not exist, expected. behaviour claim, have remove -o ${tests_prg} @ end of 'tests' rule recipe line. reason following:

you cannot specify multiple output files (-o) generate multiple programs 1 call compiler. if remove -o ${tests_prg}, compiler tries create 1 program object files specified (${tests_obj}) , each of them have main() routine, since supposed different programs. need 1 call compiler per program.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -