User Tools

Site Tools


code:work:generic:cmake:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code:work:generic:cmake:start [2024/08/13 16:53] – [Example Structure Build] davidcode: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 37: Line 39:
   - build   - build
  
 +==== Adding Library ====
 +
 +  - Add a **lib** directory and now our file looks like this <code>
 +project(lvis24)
 +cmake_minimum_required(VERSION 3.0)
 +
 +add_subdirectory(bin)
 +add_subdirectory(lib)
 +</code>
 +  - in **lib** just add the sub directory also <code>
 +# cat CMakeLists.txt 
 +add_subdirectory( hello_lib )
 +</code>
 +  - in the actual library directory we add a few things <code>
 +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)
 +</code>
 +  - back in hello world binary now we need to add this library for compile time <code>
 +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)
 +</code>
 +
 +Does this work? (spoiler alert... yep!) <code>
 +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!
 +
 +
 +</code>
  
 ==== Example With Lib ==== ==== 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.   * {{ :code:work:generic:cmake:lvis24_cmake_source_tree_example.tar.gz |}} very basic hello world and hello world library cmake skeleton example.
  
  
code/work/generic/cmake/start.1723568018.txt.gz · Last modified: 2024/08/13 16:53 by david