I have the following directory structure.
root
--src
---tests
src
contains the source & header files (C files) for the application. When this application is built, it generates an executable. tests
directory contains unit test cases (C++ files, using UnitTest++
as testing framework) for the application.
In the testing project, I can include header files from src
directory and compile will pass. Problems occur at the link time. Linker won't be able find the object files in the source directory.
How can I solve this? What is the 开发者_如何学Gonormal practice in C & C++ projects for having one executable for main application and other one for tests where both needs the same source files to work with?
- Application type : Cross platform.
- Current development env : Linux
- Build tool : CMake
Any help would be great!
What I've always done for this is had three projects. I'd have one build setup which builds a static library, containing most of my code. Then I'd have a test project that links with the static library and a project that contains UI code and such that isn't typically unit tested.
Because both projects share the same compiled static library, no recompilation of the files between the projects is necessary.
- Note: When I say "projects" above, I mean whatever has the scope of a "project" for your build system. For Visual Studio that's going to be a proj file, for CMake it should be a build target.
I would think you either need to compile those files in with your test project or create a lib in the main project which you can include.
精彩评论