CMakeLists_1
PROJECT(hello_2)
cmake_minimum_required(VERSION 2.8) #cmake的最低版本
FILE(GLOB SOURCE_1 "${CMAKE_SOURCE_DIR}/main/*.cpp") #下面2句告诉CMake,源文件在哪里?main.cpp
FILE(GLOB SOURCE_2 "${CMAKE_SOURCE_DIR}/hello/*.cpp")#程序必须链接到hello.cpp里面的SayHello
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include/")#告诉CMake头文件在哪里?
ADD_EXECUTABLE(hello_2 ${SOURCE_1} ${SOURCE_2}) #生成可执行文件
CMakeLists_2
PROJECT(hello)
cmake_minimum_required(VERSION 2.8)
SET(PROJECT_ROOT_PATH "${CMAKE_SOURCE_DIR}/../") #工程的根目录,即test
SET(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin/") #可执行文件生成后存放的目录(CMAKE_SOURCE_DIR是cmake目录)
SET(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib/") #静态库生成后存放的目录
INCLUDE_DIRECTORIES("${PROJECT_ROOT_PATH}/include/") #告诉CMake头文件在哪里?
LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/lib/")#告诉CMake静态库在哪里?
ADD_SUBDIRECTORY(src) #多目录,把src目录加进来,src里面才是真正编译main和hello的
- src中
-
ADD_SUBDIRECTORY(main)
ADD_SUBDIRECTORY(hello)
- hello中
FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/hello/hello.cpp") #告诉CMake,源文件在哪里?
ADD_LIBRARY(hello STATIC ${SOURCE_1}) #打包成hello静态库 *.a---如果想生成动态库*.so,将STATIC变为SHARED
- main中
FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/main/main.cpp")
ADD_EXECUTABLE(main ${SOURCE_1}) #生成可执行文件
TARGET_LINK_LIBRARIES(main hello) #并在可执行文件上连接到这个库