原文地址:http://blog.csdn.net/yongshengfree/article/details/4541975
GDC注:该篇文章正如其名,只是一个示例,没有讲述知识点。大家可以参考这个示例来构建自己的cmake工程目录。
cmake作为一款跨平台的项目构建工具,其使用范围已经越来越广,本文以实际项目为例,展示一个使用该编译工具的模板。希望我们都能在这些高效的工具帮助下都享受编程的乐趣。
源代码目录结构,apps目录放置可执行程序的源代码文件,modules目录存放模块源代码文件(以动态链接库的形式编译),libs目录是一些工具类的存放路径。
project
|------src
| |----apps
| |----libs
| |----modules
|------includes
|------build
|------bin
|------CMakeLists
根目录下CMakeLists.txt文件的内容
- #项目工具
- PROJECT(project)
- cmake_minimum_required(VERSION 2.6)
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")
- SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O2")
- add_subdirectory(src)
project->src目录下的CMakeLists.txt文件的内容:
- add_subdirectory(modules)
- add_subdirectory(libs)
- add_subdirectory(apps)
project->src->apps目录下CMakeLists.txt文件的内容:
- set(myexe_EXEC_SRCS
- main.cpp
- # 其它cpp文件
- )
- include_directories(
- ${PROJECT_SOURCE_DIR}/src/includes
- ${PROJECT_SOURCE_DIR}/src/libs/markup
- ${PROJECT_SOURCE_DIR}/src/modules/
- /opt/Ice/include)
- link_directories(
- /usr/local/lib
- $ENV{ORACLE_HOME}/lib
- ${PROJECT_SOURCE_DIR}/src/libs)
- link_libraries(myexe markup)
- link_libraries(myexe classloader)
- add_executable(myexe ${myexe_EXEC_SRCS})
- set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin/)
- install(TARGETS myexe RUNTIME DESTINATION bin)
project->src->libs目录下CMakeLists.txt文件的内容:
- set(utils_LIB_SRCS
- util.cpp
- #其它cpp文件
- )
- include_directories(
- ${PROJECT_SOURCE_DIR}/src/includes
- ${PROJECT_SOURCE_DIR}/src/libs/markup
- ${PROJECT_SOURCE_DIR}/src/libs)
- link_directories(
- /usr/local/lib
- #其它库路径
- )
- link_libraries(utils markup)
- link_libraries(utils uuid)
- #生成静态库
- add_library (utils STATIC ${utils_LIB_SRCS})
- set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/)
project->src->modules目录下CMakeLists.txt文件的内容:
- include_directories(
- ${PROJECT_SOURCE_DIR}/src/includes
- ${PROJECT_SOURCE_DIR}/src/libs/markup
- ${PROJECT_SOURCE_DIR}/src/libs
- )
- link_directories(
- /usr/local/lib
- )
- link_libraries(mymod markup)
- link_libraries(mymod utils)
- #生成动态链接库
- add_library (mymod SHARED mymod1.cpp mymod2.cpp)
- set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/)
- SET_TARGET_PROPERTIES(mymod PROPERTIES VERSION 1.0.0 SOVERSION 0)
- install(TARGETS mymod LIBRARY DESTINATION lib)
编译工程时直接到build目录下,执行cmake ..,然后执行make就可以了。