Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support building shared/static libraries #443 #445

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(CMAKE_CXX_STANDARD 11)

option(CGRAPH_BUILD_FUNCTIONAL_TESTS "Enables builds of functional tests" OFF)
option(CGRAPH_BUILD_PERFORMANCE_TESTS "Enables builds of performance tests" OFF)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

# 如果开启此宏定义,则CGraph执行过程中,不会在控制台打印任何信息
# add_definitions(-D_CGRAPH_SILENCE_)
Expand Down
32 changes: 30 additions & 2 deletions cmake/CGraph-env-include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
# 根据当前 CGraph-env-include.cmake 文件的位置,定位CGraph整体工程的路径
# 从而解决并兼容了直接引入和三方库引入的路径不匹配问题
get_filename_component(CGRAPH_PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
set(CGRAPH_PROJECT_ROOT_DIR "${CGRAPH_PROJECT_CMAKE_DIR}/../")

set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")

set(CGRAPH_PROJECT_ROOT_DIR "${CGRAPH_PROJECT_CMAKE_DIR}/..")
file(GLOB_RECURSE CGRAPH_PROJECT_SRC_LIST "${CGRAPH_PROJECT_ROOT_DIR}/src/*.cpp")
file(GLOB_RECURSE CGRAPH_PROJECT_HDR_LIST "${CGRAPH_PROJECT_ROOT_DIR}/src/*.h" "${CGRAPH_PROJECT_ROOT_DIR}/src/*.inl")

IF(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -O2 \
Expand All @@ -17,6 +25,7 @@ ELSEIF(UNIX)
add_definitions(-D_ENABLE_LIKELY_)
ELSEIF(WIN32)
IF(MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)
# windows平台,加入utf-8设置。否则无法通过编译
add_definitions(/utf-8)
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
Expand All @@ -33,6 +42,25 @@ ENDIF()
include_directories(${CGRAPH_PROJECT_ROOT_DIR}/src/) # 直接加入"CGraph.h"文件对应的位置

# 以下三选一,本地编译执行,推荐OBJECT方式
add_library(CGraph OBJECT ${CGRAPH_PROJECT_SRC_LIST}) # 通过代码编译
# add_library(CGraph OBJECT ${CGRAPH_PROJECT_SRC_LIST}) # 通过代码编译
# add_library(CGraph SHARED ${CGRAPH_PROJECT_SRC_LIST}) # 编译libCGraph动态库
# add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST}) # 编译libCGraph静态库
if(BUILD_SHARED_LIBS)
add_library(CGraph SHARED ${CGRAPH_PROJECT_SRC_LIST})
else()
add_library(CGraph STATIC ${CGRAPH_PROJECT_SRC_LIST})
endif()

if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
install(TARGETS CGraph
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
endif()
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
foreach ( file ${CGRAPH_PROJECT_HDR_LIST} )
get_filename_component( dir ${file} DIRECTORY )
string(REGEX REPLACE "^${CGRAPH_PROJECT_ROOT_DIR}/src[/]?" "" output_path "${dir}")
install(FILES ${file} DESTINATION "${INSTALL_INC_DIR}/${output_path}" )
endforeach()
endif()
Loading