User Tools

Site Tools


code:work:generic:cmake:start

This is an old revision of the document!


Layout

Example Structure Build

Core Hello World

Sample structure layout

  • project
    • bin
      • utils
      • tests
        • hello_world
    • build
    • include
    • lib
  1. make a folder for your project and here is the top layer CMakeLists.txt file
    project(lvis24)
    cmake_minimum_required(VERSION 3.0)
    
    add_subdirectory( bin )
  2. create a .gitignore to exclude build folder so repos/project/.gitignore should contain this
    # ignore the build directory
    build/*
  3. CMakeLists.txt in bin
    add_subdirectory( tests )
  4. CMakeLists.txt in tests
    add_subdirectory( hello_world )

Now you can compile it all by

  1. going to ~/project/build
  2. cmake ..
  3. build

Adding Library

  1. Add a lib directory and now our file looks like this
    project(lvis24)
    cmake_minimum_required(VERSION 3.0)
    
    add_subdirectory(bin)
    add_subdirectory(lib)
  2. in lib just add the sub directory also
    # cat CMakeLists.txt 
    add_subdirectory( hello_lib )
  3. in the actual library directory we add a few things
    project( hello_world_lib )
    
    add_library(${PROJECT_NAME} hello_world_lib.cpp hello_world_lib.h)
    
    # -shared creates the shared library
    set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-O2 -Wall -shared")
    
    # if you wanted to add other libs
    # target_link_libraries(${PROJECT_NAME} crypto ssl z)
  4. back in hello world binary now we need to add this library for compile time
    project(hello_world)
    cmake_minimum_required(VERSION 3.0)
    
    add_executable(${PROJECT_NAME} main.cpp)
    
    target_link_libraries(${PROJECT_NAME} hello_world_lib)
    
    include_directories(../../../lib/hello_lib)
    link_directories(../../../lib/hello_lib)

Does this work? (spoiler alert… yep!)

lvis@lvis-dell-xps:~/repos/lvis24/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lvis/repos/lvis24/build
lvis@lvis-dell-xps:~/repos/lvis24/build$ make
Scanning dependencies of target hello_world_lib
[ 25%] Building CXX object lib/hello_lib/CMakeFiles/hello_world_lib.dir/hello_world_lib.cpp.o
[ 50%] Linking CXX static library libhello_world_lib.a
[ 50%] Built target hello_world_lib
Scanning dependencies of target hello_world
[ 75%] Building CXX object bin/tests/hello_world/CMakeFiles/hello_world.dir/main.cpp.o
[100%] Linking CXX executable hello_world
[100%] Built target hello_world

lvis@lvis-dell-xps:~/repos/lvis24/build$ ./bin/tests/hello_world/hello_world 
Hello World

Hello from the library that you included!

Example With Lib

code/work/generic/cmake/start.1723568535.txt.gz · Last modified: 2024/08/13 17:02 by david