====== Layout ====== * [[https://cliutils.gitlab.io/modern-cmake/chapters/basics/structure.html]] <- good example layout ===== Example Structure Build ===== ==== Core Hello World ==== Sample structure layout * project * bin * utils * tests * hello_world * build * include * lib - 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 ) - create a .gitignore to exclude **build** folder so repos/project/.gitignore should contain this # ignore the build directory build/* - CMakeLists.txt in bin add_subdirectory( tests ) - CMakeLists.txt in tests add_subdirectory( hello_world ) Now you can compile it all by - going to ~/project/build - cmake .. - build ==== Adding Library ==== - 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) - in **lib** just add the sub directory also # cat CMakeLists.txt add_subdirectory( hello_lib ) - 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) - 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 ==== NOTE: This source also has a skeleton of a class so you can see how to create and call your own class. * {{ :code:work:generic:cmake:lvis24_cmake_source_tree_example.tar.gz |}} very basic hello world and hello world library cmake skeleton example.