code:work:generic:cmake:start
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code:work:generic:cmake:start [2024/08/13 14:09] – created david | code:work:generic:cmake:start [2024/08/13 17:02] (current) – [Example With Lib] david | ||
---|---|---|---|
Line 4: | Line 4: | ||
===== Example Structure Build ===== | ===== Example Structure Build ===== | ||
+ | |||
+ | ==== Core Hello World ==== | ||
Sample structure layout | Sample structure layout | ||
Line 36: | Line 38: | ||
- cmake .. | - cmake .. | ||
- build | - 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(../ | ||
+ | link_directories(../ | ||
+ | </ | ||
+ | |||
+ | Does this work? (spoiler alert... yep!) < | ||
+ | lvis@lvis-dell-xps: | ||
+ | -- 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: / | ||
+ | -- Check for working CXX compiler: / | ||
+ | -- 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: / | ||
+ | lvis@lvis-dell-xps: | ||
+ | Scanning dependencies of target hello_world_lib | ||
+ | [ 25%] Building CXX object lib/ | ||
+ | [ 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/ | ||
+ | [100%] Linking CXX executable hello_world | ||
+ | [100%] Built target hello_world | ||
+ | |||
+ | lvis@lvis-dell-xps: | ||
+ | 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/start.1723558142.txt.gz · Last modified: 2024/08/13 14:09 by david