CMake VS工程總結

峻峰飛陽發表於2019-04-08

1.設定輸出字尾

set(CMAKE_DEBUG_POSTFIX "d")

2.設定輸出目錄

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

3.設定檔案虛擬目錄

SOURCE_GROUP(AppCore FILES ${AppCore_H_CPP})

4.設定工程虛擬目錄

add_subdirectory(子工程資料夾)
add_dependencies(子工程名 依賴其它的工程名)

SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(TARGET 子工程名 PROPERTY FOLDER "虛擬目錄名")

5.設定生成動態庫,靜態庫選項

set(BUILD_SHARED_LIBS "On")
#set(BUILD_SHARED_LIBS "Off")

6.從環境變數裡查詢庫

find_path(3rdparty_INCLUDE_DIR zlib.h ENV 3rdparty_INCLUDE_DIR)
find_path(3rdparty_LIB_DIR zlib.lib ENV 3rdparty_LIB_DIR)

7.Boost在CMake中的使用

設定環境變數 Boost_DIR

set(Boost_USE_STATIC_LIBS ON)

set(Boost_USE_MULTITHREADED  ON)
find_package(Boost 1.49 REQUIRED system thread regex date_time)

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

8.除錯庫和非除錯庫連線

target_link_libraries(debug ${} optimized ${})

 

9.輸出動態庫和靜態庫

if(${BUILD_SHARED_LIBS} STREQUAL "On")
    add_library(${OUT_NAME} SHARED ${SRC} ${HEADER} ${SOUECE})
    ADD_DEFINITIONS(-DXXX_LIBRARY)
else()
    add_library(${OUT_NAME} STATIC ${SRC} ${HEADER})
    ADD_DEFINITIONS(-DXXX_LIBRARY_STATIC)
endif()

#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) || defined( __BCPLUSPLUS__)  || defined( __MWERKS__)
    #  if defined( XXX_LIBRARY_STATIC )
    #    define XXX_EXPORT
    #  elif defined( XXX_LIBRARY )
    #    define XXX_EXPORT   __declspec(dllexport)
    #  else
    #    define XXX_EXPORT   __declspec(dllimport)
    #  endif
#else
    #  define XXX_EXPORT
#endif  

 

相關文章