How to write "CMakeLists.txt" for a large project with several subdirectories?

I am working on a modeling project: I take the built-in C code base from the target platform and try to simulate it on the host computer for debugging or a single step in the code.

OS: Ubuntu Linux 14.04, IDE: CodeLite, Generator Makefile: Cmake . I am confused about how to write a CMakeLists.txt project for a project. Below is the codebase structure (everything is written in C ):

|ARQSim\  
|-->ARQSim.h  
|-->ARQSim.c  
|-->BaseStationCode\  
|   |->downlink.c  
|   |->neoncopy.c  
|   |->armCore\  
|   |  |->common\  
|   |  |  |->Bsconfig.h  
|   |  |  |->config.h  
|   |  |->MacSource\  
|   |  |  |->lib\  
|   |  |  |  |->arqCommon.h  
|   |  |  |  |->OverTheAir.h  
|   |  |  |->source\  
|   |  |  |  |->beacon.c  
|   |  |  |  |->proxyDhcp.c  
|   |  |  |  |->ARQ\  
|   |  |  |  |  |->arqCommon.c  
|   |  |  |  |  |->arqInterface.c  
|   |  |  |  |  |->fragmentation\  
|   |  |  |  |  |  |->fragBookkeeping.c  
|   |  |  |  |  |  |->fragProcessAck.c  
|   |  |  |  |  |->reassembly\  
|   |  |  |  |  |  |->reasmBookkeeping.c  
|   |  |  |  |  |  |->reasmProcessAck.c

I am completely new to Cmake. I read a lot of resources on CMake and threads here in StackOverflow. But I am embarrassed every time. I have a few questions:

  • CMakeLists.txt CMakeLists.txt?
  • CMakeLists.txt?
  • , CMakeLists.txt MakeFile?

. , - , . .

+4
1

CMakeLists.txt CMakeLists.txt?

,

:

root/
+--- CMakeLists.txt             // your root CMakeLists
+--- foo/
|    +--- CMakeLists.txt        // foo component CMakeLists
|    +--- foo.c
|    +--- tests/
|         +--- CMakeLists.txt   // foo test CMakeLists
|         +--- foo_tests.c
+--- bar/
     +--- CMakeLists.txt        // bar component CMakeLists
     +--- bar.c
     +--- bar_impl/             // no CMakeLists for this dir, it is part of bar
     |    +--- bar_impl.c
     +--- tests/
          +--- CMakeLists.txt   // bar test CMakeLists
          +--- bar_tests.c

CMakeLists.txt:

CMakeLists.txt cmake, , .

root/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)
project (my_project C)

add_subdirectory(foo)
add_subdirectory(bar)

CMakeLists.txt:

CMakeLists.txt , , ..

root/foo/CMakeLists.txt:

add_library(foo foo.c)
target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(tests)

root/foo/tests/CMakeLists.txt:

add_executable(foo_test foo_tests.c)
target_link_libraries(foo_test foo)

..

root/foo/CMakeLists.txt:

add_library(bar 
    bar.c 
    bar_impl/bar_impl.c)
target_include_directories(bar PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(bar foo)

add_subdirectory(tests)

root/bar/tests/CMakeLists.txt:

add_executable(bar_test bar_tests.c)
target_link_libraries(bar_test bar)

:

, cmake root/CMakeLists.txt

cd root
mkdir build
cd build
cmake ..

( ide )

, , . :

, :

CMakeLists.txt?

(. ).

, .

, , , CMakeLists.txt -

:

foo/
+--- foo.c
+--- bar.c
+--- baz/
     +--- baz.c
     +--- bang.c

foo, :

add_library(foo 
   foo.c
   bar.c
   baz/baz.c
   baz/bang.c)

SRCS

set(SRCS 
   foo.c
   bar.c
   baz/baz.c
   baz/bang.c)

add_library(foo ${SRCS})
+7

Source: https://habr.com/ru/post/1688896/


All Articles