tags:

views:

88

answers:

2

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 normal 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!

+2  A: 

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.

Jon Cage
Yes. I was thinking about compiling them in test project. But this will lead into a file compiled twice. That doesn't look good to me.
Appu
Compile the core code you want to test as a lib then and call it from both your test project and your main calling code.
Jon Cage
+4  A: 

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.
Billy ONeal
That's exactly the same approach I tend to end up using.
Jon Cage
I understand you point. Technically, is there a difference in creating a static library and executable linking to it and linking object files directly to the executable?
Appu
It should make no difference Appu.
Jon Cage
@John Cage: I'm sorry, but that's not what I understood from your answer. I'll +1 because I didn't mean to steal your answer here, but I do note that taking a little time to write out your answer in plain English instead of abbreviating it goes a long way toward helping others understand you.
Billy ONeal
I wouldn't call it stealing - if you can answer it more clearly (and I think in this case you did) then you should go for it. The whole point of SO is that the most accurate, clear to understand, and useful answers should bubble to the top. Re-reading my answer now I can see how it might sound a little brief ;-)
Jon Cage