Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:46:22+00:00 2026-06-17T14:46:22+00:00

I am trying to structure my project to include the production sources (in src

  • 0

I am trying to structure my project to include the production sources (in src subfolder) and tests (in test subfolder). I am using CMake to build this. As a minimal example I have the following files:

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8) 
project (TEST) 

add_subdirectory (src) 
add_subdirectory (test) 

src/CMakeLists.txt:

add_executable (demo main.cpp sqr.cpp) 

src/sqr.h

#ifndef SQR_H
#define SQR_H
double sqr(double);    
#endif // SQR_H

src/sqr.cpp

#include "sqr.h"
double sqr(double x) { return x*x; }

src/main.cpp – uses sqr, doesn’t really matter

test/CMakeLists.txt:

find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)

include_directories (${TEST_SOURCE_DIR}/src) 

ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) 

add_executable (test test.cpp ${TEST_SOURCE_DIR}/src/sqr.cpp) 

target_link_libraries(test
                      ${Boost_FILESYSTEM_LIBRARY}
                      ${Boost_SYSTEM_LIBRARY}
                      ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                      )

enable_testing()
add_test(MyTest test)

test/test.cpp:

#define BOOST_TEST_MODULE SqrTests
#include <boost/test/unit_test.hpp>

#include "sqr.h"

BOOST_AUTO_TEST_CASE(FailTest)
{
    BOOST_CHECK_EQUAL(5, sqr(2));
}

BOOST_AUTO_TEST_CASE(PassTest)
{
    BOOST_CHECK_EQUAL(4, sqr(2));
}

A few questions:

  1. Does this structure make sense? What are the best practises when structuring this code? (I am coming from C# and java, and there it is easier in a sense)
  2. I don’t like the fact that I have to list all the files from the src folder in the test/CMakeLists.txt file. If this was a library project, I would just link the library. Is there a way to avoid listing all the cpp files from the other project?
  3. What are the lines enable_testing() and add_test(MyTest test) doing? I haven’t seen any effect. How can I run the tests from CMake (or CTest)?
  4. So far I just ran cmake . in the root folder, but this created a mess with temporary files everywhere. How can I get the compilation results in a reasonable structure?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:46:23+00:00Added an answer on June 17, 2026 at 2:46 pm

    For questions 1 & 2, I would recommend making a library from your non-test files excluding main.cpp (in this case just src/sqr.cpp and src/sqr.h), and then you can avoid listing (and more importantly re-compiling) all the sources twice.

    For question 3, these commands add a test called "MyTest" which invokes your executable "test" without any arguments. However, since you’ve added these commands to test/CMakeLists.txt and not your top-level CMakeLists.txt, you can only invoke the test from within the "test" subdirectory of your build tree (try cd test && ctest -N). If you want the test to be runnable from your top-level build directory, you’d need to call add_test from the top-level CMakeLists.txt. This also means you have to use the more verbose form of add_test since your test exe isn’t defined in the same CMakeLists.txt

    In your case, since you’re running cmake in the root folder, your build tree and your source tree are one and the same. This is known as an in-source build and isn’t ideal, which leads to question 4.

    The preferred method for generating the build tree is to do an out-of-source build, i.e. create a directory somewhere outside of your source tree and execute cmake from there. Even creating a "build" directory in the root of your project and executing cmake .. would provide a clean structure which won’t interfere with your source tree.

    One final point is to avoid calling executables "test" (case-sensitive). For reasons why, see this answer.

    To achieve these changes, I’d do the following:

    CMakeLists.txt:

    cmake_minimum_required (VERSION 2.8)
    project (TEST)
    add_subdirectory (src) 
    add_subdirectory (test)
    enable_testing ()
    add_test (NAME MyTest COMMAND Test)
    

    src/CMakeLists.txt:

    add_library (Sqr sqr.cpp sqr.h)
    add_executable (demo main.cpp)
    target_link_libraries (demo Sqr)
    

    test/CMakeLists.txt:

    find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
    include_directories (${TEST_SOURCE_DIR}/src
                         ${Boost_INCLUDE_DIRS}
                         )
    add_definitions (-DBOOST_TEST_DYN_LINK)
    add_executable (Test test.cpp)
    target_link_libraries (Test
                           Sqr
                           ${Boost_FILESYSTEM_LIBRARY}
                           ${Boost_SYSTEM_LIBRARY}
                           ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                           )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to include a folder structure into my xcode project that contains
I am trying to include a library into my android project using IntelliJ IDEA
I have a project using autoconf and automake with following structure: / src/ class.h
I am trying to configure the NCommon NHRepository in my project with Structure Map.
I am trying to create a wizard like structure using dialog boxes...So I replaced
I am trying to build a Quadtree data structure(or let's just say a tree)
I'm trying to ignore the bin folder of a web project, but include the
I'm trying to convert a Netbeans 6.9.1 project into a scripted build (without netbeans).
I am trying to implement the project structure as explained in the following blog:
I have a following directory structure in my project: bin/ dist/ include/ ├── module_a/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.