首页 > 代码库 > CMake VS工程总结

CMake VS工程总结

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