cmake配置c++生成dll

  • Post author:
  • Post category:build
  • Post comments:0评论
cmake_minimum_required(VERSION 3.14)
# name 
set(MY_PROJECT_NAME mmbase)
#
project(${MY_PROJECT_NAME} LANGUAGES CXX)
#
message ("cmake:${PROJECT_NAME}")
#
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# debug suffix -d
set(CMAKE_DEBUG_POSTFIX -d)
# copy to
set(MY_SOURCE_LIB_DIR "${PROJECT_SOURCE_DIR}/../lib/")
# default Release
if (NOT CMAKE_BUILD_TYPE)
   set (CMAKE_BUILD_TYPE "Release")
   message (
      STATUS "${PROJECT_NAME} no CMAKE_BUILD_TYPE selected, defaulting to ${CMAKE_BUILD_TYPE} "
   )
endif()
# 
add_library(${PROJECT_NAME} SHARED
  logs.h
  logs.cpp
)
# windows c++ dll导出宏
target_compile_definitions(${PROJECT_NAME} PRIVATE MMBASE_LIBRARY=1)
# vs unicode
if(MSVC)
  add_definitions("-DUNICODE" "-D_UNICODE")
  target_compile_options(${PROJECT_NAME} PUBLIC "/ZI")
  target_link_options(${PROJECT_NAME} PUBLIC "/INCREMENTAL")
endif()
# 生成后复制
if (WIN32) 
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        # Adds a post-build event to MyTest
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
        "$<$<CONFIG:Debug>:debug/${PROJECT_NAME}-d.lib>"      # <--this is in-file
        "$<$<CONFIG:Release>:release/${PROJECT_NAME}.lib>"      # <--this is in-file
        "${MY_SOURCE_LIB_DIR}")                 # <--this is out-file path
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD    
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  
        "$<$<CONFIG:Debug>:debug/${PROJECT_NAME}-d.dll>"  
        "$<$<CONFIG:Release>:release/${PROJECT_NAME}.dll>"  
        "${MY_SOURCE_LIB_DIR}")  
else() # (WIN32) 
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  
        "$<$<CONFIG:Debug>:lib${PROJECT_NAME}-d.so>"  
        "$<$<CONFIG:Release>:lib${PROJECT_NAME}.so>"       
        "${MY_SOURCE_LIB_DIR}")                
endif() # (WIN32) 

导出类声名

#if defined (WIN32) || defined(WIN64)
// windows
#if defined(MMBASE_LIBRARY)
#  define MMBASESHARED_EXPORT __declspec(dllexport)
#else
#  define MMBASESHARED_EXPORT __declspec(dllimport)
#endif

#else //  defined (WIN32) || defined(WIN64)
// linux
#  define MMBASESHARED_EXPORT  

#endif //  defined (WIN32) || defined(WIN64)

class MMBASESHARED_EXPORT  Logs
{

}
文章作者: 张拓
文章链接: http://www.xssl.online/cmake%e9%85%8d%e7%bd%aec%e7%94%9f%e6%88%90dll/
版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明来自 张拓的博客
浏览次数: 776

张拓

陕西西安蓝田张拓QQ1070410059。一生所求不过“心安”二字。 然,尘世多纷扰。

发表回复