cmake的基础知识:CMakeLists常用命令,在这里不再赘述。
Windows平台下可用cmake-gui生成vs的.sln工程,Linux平台下可以运行cmake命令。
动态库和静态库的构建
现有C++工程目录结构如下:
静态库的构建
add.h
#include <iostream>
int add(int a, int b);
add.cpp
#include "add.h"
int add(int a, int b)
{
return a+b;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(add)
add_library(add_static STATIC add.h add.cpp)
在Windows平台下生成add_static.lib;在Linux平台下生成add_static.a。
动态库的构建
在Linux平台下构建动态库的方法和静态库生成的方法类似,只有在使用add_library命令时,参数STATIC改为SHARE即可。
在Windows平台下,在导出动态库时除了会生成.dll动态库之外,还会生成一个.lib文件。注意,这个.lib文件和静态库的.lib文件时不同的,它里面并不保存代码生成的二进制文件,而是所有需要导出符号的符号表。因此这个.lib文件和编译生成的的静态库.lib相比较而言会小的多。在Windows平台下导出dll动态库时若无__declspec(dllexport),依然可以成功的编译出动态库,但是并不会生成保存符号表的.lib文件。
add.h
#include <iostream>
#ifdef _WIN32
__declspec(dllexport) int add(int a, int b);
#endif
#ifdef __linux__
int add(int a, int b);
#endif
add.cpp
#include "add.h"
int add(int a, int b)
{
return a+b;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(add)
add_library(add_shared SHARED add.h add.cpp)
在Windows平台下生成add_shared.dll和add_shared.lib;在Linux平台下生成add_shared.so。
动态库和静态库的链接
现有C++工程目录结构如下(其中dll和lib文件是在上一节中生成的):
静态库的链接
add.h
#include <iostream>
int add(int a, int b);
main.cpp
#include <iostream>
#include "add.h"
int main()
{
std::cout << add(1, 2) << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(use_add)
add_executable(use_add main.cpp add.h)
target_link_libraries(use_add ${CMAKE_SOURCE_DIR}/add_static.lib)
动态库的链接
add.h
#include <iostream>
#ifdef _WIN32
__declspec(dllexport) int add(int a, int b);
#endif
#ifdef __linux__
int add(int a, int b);
#endif
main.cpp
#include <iostream>
#include "add.h"
int main()
{
std::cout << add(1, 2) << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(use_add)
add_executable(use_add main.cpp add.h)
target_link_libraries(use_add ${CMAKE_SOURCE_DIR}/add_shared.lib)
运行的时候需要把add_shared.dll复制到工程目录中。
综合应用
包含子目录的工程
现有C++工程目录结构如下:
这种目录组织结构在工作中很常见,include文件夹下是头文件,source文件夹下是源文件。
add.h
#include <iostream>
#ifdef _WIN32
__declspec(dllexport) int add(int a, int b);
#endif
#ifdef __linux__
int add(int a, int b);
#endif
subtract.h
#include <iostream>
#ifdef _WIN32
__declspec(dllexport) int subtract(int a, int b);
#endif
#ifdef __linux__
int subtract(int a, int b);
#endif
add.cpp
#include "add.h"
int add(int a, int b)
{
return a+b;
}
subtract.cpp
#include "subtract.h"
int subtract(int a, int b)
{
return a-b;
}
main.cpp
#include <iostream>
#include "add.h"
#include "subtract.h"
int main()
{
std::cout << add(1, 2) << std::endl;
std::cout << subtract(1, 2) << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_executable(test main.cpp ${INCS} ${SRCS})
若需要将include和source文件夹下的内容编成库,需要修改CMakeLists.txt。
生成静态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_static STATIC ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_static)
生成动态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_shared SHARED ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_shared)
既构建库又链接库的工程
现有C++工程目录结构如下:
其中dll和lib文件是之前构建的库,和上一小节不同在于,少了add.cpp,改为由前面编好的动态库或者静态库提供定义,其他的头文件和源文件内容和上一小节的相同。
下面是一个可以编译该工程的CMakeLists.txt:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_executable(test main.cpp ${INCS} ${SRCS})
#target_link_libraries(test ${CMAKE_SOURCE_DIR}/add_static.lib) #链接静态库
target_link_libraries(test ${CMAKE_SOURCE_DIR}/add_shared.lib) #链接动态库
链接静态库或者链接动态库都可以,其中链接动态库时需要把add_shared.dll放到工程目录下才能正常运行可执行程序。下面修改CMakeLists.txt,既链接库又构建库,有一点点绕哦,做好准备~
链接静态库并构建静态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_static STATIC ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_static ${CMAKE_SOURCE_DIR}/add_static.lib)
链接动态库并构建静态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_static STATIC ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_static ${CMAKE_SOURCE_DIR}/add_shared.lib)
链接静态库并构建动态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_shared SHARED ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_shared ${CMAKE_SOURCE_DIR}/add_static.lib)
链接动态库并构建动态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_shared SHARED ${INCS} ${SRCS})
add_executable(test main.cpp)
target_link_libraries(test test_shared ${CMAKE_SOURCE_DIR}/add_shared.lib)
hh~ 看到这里不知道大家有没有被绕晕了呢…确实嗷,LZ当时为了弄清楚这些也是花了一个晚上~
在工作中,有时候还需要将现有的静态库/动态库编入自己的静态库/动态库中,继续改写上面的CMakeLists.txt。
链接静态库并构建静态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_static STATIC ${INCS} ${SRCS})
target_link_libraries(test_static ${CMAKE_SOURCE_DIR}/add_static.lib)
add_executable(test main.cpp)
target_link_libraries(test test_static)
链接动态库并构建静态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_static STATIC ${INCS} ${SRCS})
target_link_libraries(test_static ${CMAKE_SOURCE_DIR}/add_shared.lib)
add_executable(test main.cpp)
target_link_libraries(test test_static)
链接静态库并构建动态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_shared SHARED ${INCS} ${SRCS})
target_link_libraries(test_shared ${CMAKE_SOURCE_DIR}/add_static.lib)
add_executable(test main.cpp)
target_link_libraries(test test_shared)
链接动态库并构建动态库:
cmake_minimum_required (VERSION 2.8)
project(test)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/source)
aux_source_directory(${PROJECT_SOURCE_DIR}/source SRCS)
file(GLOB INC_PATH ${PROJECT_SOURCE_DIR}/include/*.h)
list(APPEND INCS ${INC_PATH})
add_library(test_shared SHARED ${INCS} ${SRCS})
target_link_libraries(test_shared ${CMAKE_SOURCE_DIR}/add_shared.lib)
add_executable(test main.cpp)
target_link_libraries(test test_shared)