Reputation: 679
My project is something like this:
project
├── test
│ ├── CMakeLists.txt
│ ├── main.cpp
│ └── test_class1.cpp
├── CMakeLists.txt
├── main.cpp
├── ...
├── class1.h
└── class1.cpp
I want to reuse class1.o which was compiled for project binary. Default CMake behavior compiled it twice for test too. I try to use OBJECT library but it puts all objects in this library variable. Then compiler print
main.cpp:(.text.startup+0x0): multiple definition of `main'
It means that 2 different main.o in one target. And all other *.o files from main project compilation are.
How to exlude unneeded *.o files?
Upvotes: 1
Views: 1885
Reputation: 6066
What I normally do is that I separate the application logic into a static library (subdirectory), which is then used by both the main application and the tests.
You could keep it in a single dir as well, but then you still need to exclude the main.cpp when building the object library, because otherwise you will indeed have multiple definitions of main
when building tests (as defined in both main.cpp and the test main).
If you are listing the files explicitly (which is highly recommended for CMake), you can simply omit the main.cpp from the list. If you use globbing to list the files, you can remove the file from the list as described here: How do I exclude a single file from a cmake `file(GLOB ... )` pattern?
Upvotes: 5