tswanson-cs
tswanson-cs

Reputation: 116

How to structure cmake project with 2 main src files

I would like to understand how to structure my cpp project correctly. I am using the build generator CMAKE. The build system I am using is Ninja. I have 2 main functions in my project. Each main should be compiled into a different executable.

When and why should I use multiple cmake files?
How can I better structure my project?

    |-- CMakeLists.txt
    |-- README.md
    |-- env.csh
    |-- include
    |   |-- Pen.h
    |   |-- Cup.h
    |   |-- Clip.h
    |   |-- Fun.h
    |   |-- Ins.h
    |   |-- Ne.h
    |   `-- Pa.h
    |-- libs
    |-- src
    |   |-- Pen.cpp
    |   |-- Cup.cpp
    |   |-- Clip.cpp
    |   |-- Fun.cpp
    |   |-- Ins.cpp
    |   |-- Ne.cpp
    |   |-- Pa.cpp
    |   |-- main0.cpp
    |   `-- main1.cpp
    `-- tests
        `-- test.cpp

Upvotes: 0

Views: 238

Answers (1)

Enno
Enno

Reputation: 1862

You need one add_executable() line for each executable in your project. Try this CMakeLists.txt file (written mostly from memory):

cmake_minimum_required(VERSION 2.8)
project(myproject LANGUAGES CXX)
enable_testing()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SOURCES 
    src/Pen.cpp 
    src/Cup.cpp  
    src/Clip.cpp  
    src/Fun.cpp  
    src/Ins.cpp  
    src/Ne.cpp  
    src/Pa.cpp
)
add_executable(main0 src/main0.cpp ${SOURCES})
add_executable(main1 src/main1.cpp ${SOURCES})
add_executable(unittests tests/test.cpp ${SOURCES})
add_test(tests unittests)

Upvotes: 1

Related Questions