Skip to content

Instantly share code, notes, and snippets.

@TheBeachMaster
Last active April 27, 2018 12:46
Show Gist options
  • Save TheBeachMaster/87b8e8c27dee8b3d713595f8dc88d12f to your computer and use it in GitHub Desktop.
Save TheBeachMaster/87b8e8c27dee8b3d713595f8dc88d12f to your computer and use it in GitHub Desktop.
CMAKE Building Libraries Example
# Suppose a structure such as:
#... # root directory
#.../build
#.../lib
#.../lib/mylib.hpp
#.../lib/mylib.cc
#.../src
#.../src//main.cc
#.../CMakeLists.txt
#..... # your app .../src/main.cc
#include "../lib/mylib.hpp"
#..... # other things
# Once done with this file, navigate to build directory and run " cmake .. " as admin/root
# If everything goes well, you should get a Makefile in the build dir, then run " make " as admin/root
# You should get a main binary/executable in this build folder, run as admin/root
# If you are looking for something more advanced... go to → https://github.com/TheBeachMaster/gtest
#<=================>CMakeLists.txt
# Specify Minimum CMake version
cmake_minimum_required(VERSION 2.8.9)
# Project Name
project(mylib CXX)
# BUILD TYPE
set(CMAKE_BUILD_TYPE Release)
# Bring in any extra header files
include_directories(lib)
# Your library source files
set(LIB_SRC lib/mylib.cc)
# Generate Shared Library : Can also be STATIC
add_library(mylib SHARED ${LIB_SRC})
set(SHARED_LIB mylib)
# Create Application Bin
add_executable(main src/main.cc)
# Make symbols MSVC Compatible as well
include(GenerateExportHeader)
GENERATE_EXPORT_HEADER(${SHARED_LIB}
BASE_NAME ${SHARED_LIB}
EXPORT_MACRO_NAME ${SHARED_LIB}_EXPORTS
EXPORT_FILE_NAME ${SHARED_LIB}_EXPORTS.h
STATIC_DEFINE SHARED_EXPORTS_BUILT_AS_STATIC)
# Then link
target_link_libraries(main ${SHARED_LIB})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment