c++ - Building of executable and shared library with cmake

C++ - Building of executable and shared library with cmake

To build both an executable and a shared library using CMake, you need to set up your CMakeLists.txt file properly to define the targets, their properties, and their dependencies. Here's a step-by-step guide on how to do this:

1. Basic Project Structure

Let's assume you have the following project structure:

project/ ├── CMakeLists.txt ├── src/ │ ├── main.cpp │ ├── lib.cpp │ ├── lib.h ├── build/ 

2. Writing CMakeLists.txt

Here is a sample CMakeLists.txt file that will build both an executable and a shared library:

cmake_minimum_required(VERSION 3.10) # Minimum CMake version required project(MyProject) # Project name # Set C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # Add the shared library add_library(MyLibrary SHARED src/lib.cpp src/lib.h ) # Specify include directories for the library target_include_directories(MyLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ) # Add the executable add_executable(MyExecutable src/main.cpp ) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MyLibrary) 

Explanation

  1. CMake Version and Project Name:

    cmake_minimum_required(VERSION 3.10) project(MyProject) 
  2. Set C++ Standard:

    set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) 

    Adjust the C++ standard as needed.

  3. Add the Shared Library:

    add_library(MyLibrary SHARED src/lib.cpp src/lib.h ) 
    • MyLibrary is the name of the shared library.
    • SHARED specifies that this is a shared library.
  4. Specify Include Directories:

    target_include_directories(MyLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ) 

    This tells CMake where to find the header files for MyLibrary.

  5. Add the Executable:

    add_executable(MyExecutable src/main.cpp ) 
  6. Link the Executable to the Shared Library:

    target_link_libraries(MyExecutable PRIVATE MyLibrary) 

    This links MyExecutable with MyLibrary.

3. Building the Project

To build the project, follow these steps:

  1. Navigate to the build Directory:

    cd build 
  2. Run CMake to Configure the Project:

    cmake .. 
  3. Build the Project:

    cmake --build . 

This will generate both the shared library (libMyLibrary.so on Unix-like systems or MyLibrary.dll on Windows) and the executable (MyExecutable) in the build directory.

Additional Considerations

  • Platform-Specific Details: The naming conventions for shared libraries may differ by platform (e.g., .so on Linux, .dylib on macOS, and .dll on Windows).

  • Debugging: You might want to add debugging symbols by setting CMAKE_BUILD_TYPE to Debug:

    set(CMAKE_BUILD_TYPE Debug) 
  • Dependencies: If your shared library has dependencies on other libraries, make sure to specify those using target_link_libraries.

This setup provides a basic example of how to configure CMake for building both an executable and a shared library. You can adjust and expand this configuration as needed for more complex projects.

Examples

  1. "CMake build shared library and executable in same project"

    Description: Configure a CMake project to build both a shared library and an executable that links to this library.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MyLibrary SHARED src/mylibrary.cpp) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the library target_link_libraries(MyExecutable PRIVATE MyLibrary) 
  2. "CMake create static and shared libraries in the same project"

    Description: Set up a CMake project to build both static and shared versions of a library.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyLibraryProject) # Create a static library add_library(MyStaticLibrary STATIC src/mylibrary.cpp) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  3. "CMake target_link_libraries for shared library"

    Description: Use target_link_libraries to link a shared library to an executable in CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  4. "CMake include directories for shared library"

    Description: Add include directories for a shared library using CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Specify include directories target_include_directories(MySharedLibrary PUBLIC ${CMAKE_SOURCE_DIR}/include) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  5. "CMake add_library for multiple source files"

    Description: Create a shared library from multiple source files using CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library from multiple source files add_library(MySharedLibrary SHARED src/file1.cpp src/file2.cpp) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  6. "CMake build shared library with custom output directory"

    Description: Set a custom output directory for the shared library built with CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Set custom output directory for shared libraries set_target_properties(MySharedLibrary PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  7. "CMake define library version for shared library"

    Description: Define the version for a shared library in CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Define library version set(MYLIBRARY_VERSION_MAJOR 1) set(MYLIBRARY_VERSION_MINOR 0) set(MYLIBRARY_VERSION_PATCH 0) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Set version information set_target_properties(MySharedLibrary PROPERTIES VERSION ${MYLIBRARY_VERSION_MAJOR}.${MYLIBRARY_VERSION_MINOR}.${MYLIBRARY_VERSION_PATCH}) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  8. "CMake build shared library with specific compiler flags"

    Description: Apply specific compiler flags to a shared library in CMake.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Set compiler flags for the shared library target_compile_options(MySharedLibrary PRIVATE -Wall -Wextra) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  9. "CMake build shared library with different configurations"

    Description: Configure CMake to build shared libraries with different build types (Debug, Release).

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Set different compiler flags for Debug and Release target_compile_options(MySharedLibrary PRIVATE $<$<CONFIG:Debug>:-g> $<$<CONFIG:Release>:-O3> ) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 
  10. "CMake shared library with Windows-specific settings"

    Description: Configure CMake to build a shared library with Windows-specific settings such as defining EXPORT macros.

    Code:

    cmake_minimum_required(VERSION 3.10) project(MyProject) # Define EXPORT macros for Windows if(WIN32) add_definitions(-DMYLIBRARY_EXPORTS) endif() # Create a shared library add_library(MySharedLibrary SHARED src/mylibrary.cpp) # Create an executable add_executable(MyExecutable src/main.cpp) # Link the executable to the shared library target_link_libraries(MyExecutable PRIVATE MySharedLibrary) 

More Tags

android-webservice morse-code aws-cloudformation output export stylish enumerator nltk layer ssms-2017

More Programming Questions

More Chemistry Calculators

More Animal pregnancy Calculators

More Statistics Calculators

More Auto Calculators