Skip to content

Instantly share code, notes, and snippets.

@cppengineer
Last active May 30, 2022
Embed
What would you like to do?
Using CMake to create INTERFACE (header-only), SHARED and STATIC libraries and how to link to an executable
//
// Created by Jan Gabriel on 3/19/2021.
//
#include "example/example.h"
Example::Example(std::string name) : name_(std::move(name)) {}
void Example::PrintName() const {
std::cout << name_ << std::endl;
}
//
// Created by Jan Gabriel on 3/19/2021.
//
#ifndef EXAMPLE_LIB_EXAMPLE_INCLUDE_EXAMPLE_EXAMPLE_H_
#define EXAMPLE_LIB_EXAMPLE_INCLUDE_EXAMPLE_EXAMPLE_H_
#include <iostream>
#include <string>
#ifdef HEADER_ONLY
/*
* The Example class takes a name as parameter during construction
* and can print it out when its PrintName() method is called
*/
class Example {
public:
// Constructor
explicit Example(std::string name) : name_(std::move(name)) {}
// Print the name
void PrintName() const {
std::cout << name_ << std::endl;
}
private:
const std::string name_;
};
#else
/*
* The Example class takes a name as parameter during construction
* and can print it out when its PrintName() method is called
*/
class Example {
public:
// Constructor
explicit Example(std::string name);
// Print the name
void PrintName() const;
private:
const std::string name_;
};
#endif
#endif//EXAMPLE_LIB_EXAMPLE_INCLUDE_EXAMPLE_EXAMPLE_H_
## CMakeLists.txt in example lib folder ##
cmake_minimum_required(VERSION 3.0)
project(example)
# Add INTERFACE library, thus, header only
add_library(${PROJECT_NAME}_interface INTERFACE)
# Add target include libraries, thus, the header file include path
target_include_directories(${PROJECT_NAME}_interface INTERFACE ${PROJECT_SOURCE_DIR}/include)
# Add STATIC library with source example.cpp in src directory
add_library(${PROJECT_NAME}_static STATIC ${PROJECT_SOURCE_DIR}/src/example.cpp)
# Add target include libraries, thus, the header file include path
target_include_directories(${PROJECT_NAME}_static PUBLIC ${PROJECT_SOURCE_DIR}/include)
# Export symbols when on windows for making a SHARED lib
if(MSVC OR MINGW)
# This is necessary for windows, requires CMake 3.4+
cmake_minimum_required(VERSION 3.4)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)
endif()
# Add SHARED library with source example.cpp in src directory
add_library(${PROJECT_NAME}_shared SHARED ${PROJECT_SOURCE_DIR}/src/example.cpp)
# Add target include libraries, thus, the header file include path
target_include_directories(${PROJECT_NAME}_shared PUBLIC ${PROJECT_SOURCE_DIR}/include)
/**
Using CMake to create INTERFACE (header-only), SHARED and STATIC libraries
file: CMakeLists.txt
url: https://cppengineer.com
date: March 2021
*/
#include <iostream>
#include "example/example.h"
int main() {
Example example("World!");
std::cout << "Hello, ";
example.PrintName();
return 0;
}
#[[
Using CMake to create INTERFACE (header-only), SHARED and STATIC libraries
file: CMakeLists.txt
url: https://cppengineer.com
date: March 2021
]]
#
# CMakeLists.txt in main folder
cmake_minimum_required(VERSION 3.0)
project(main)
# Project structure #
#[[
+-- lib
| +-- example
| +-- include
| | +-- example
| | +-- example.h
| +-- src
| | +-- example.cpp
| +-- CMakeLists.txt
|
+-- src
| +-- main.cpp
|
+-- CMakeLists.txt
]]
#
# Add lib subdirectory (to build our libraries)
add_subdirectory(lib/example)
# Add a main executables
add_executable(${PROJECT_NAME}_header_only ${PROJECT_SOURCE_DIR}/src/main.cpp)
add_executable(${PROJECT_NAME}_shared ${PROJECT_SOURCE_DIR}/src/main.cpp)
add_executable(${PROJECT_NAME}_static ${PROJECT_SOURCE_DIR}/src/main.cpp)
# When using a header-only, thus, INTERFACE library
target_link_libraries(${PROJECT_NAME}_header_only example_interface)
# Only needed for our example #define HEADER_ONLY
target_compile_definitions(${PROJECT_NAME}_header_only PUBLIC HEADER_ONLY)
# When using a SHARED library
target_link_libraries(${PROJECT_NAME}_shared example_shared)
# When using a STATIC library
target_link_libraries(${PROJECT_NAME}_static example_static)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment