如何通过cmake在子目录中共享头文件和库?

时间:2015-06-14 12:30:45

标签: c++ cmake shared-libraries

我想将我的标题和库用作app1和app2的公共库。我的项目树在下面。 public static void main(String args[]) { { try { String transDate = "2015-04-15T12:55:07.365"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS"); Date date = sdf.parse(transDate); SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date d = sdf.parse(sdf.format(date)); String formattedTime = output.format(d); System.out.println("transDate:" + transDate + ", parsedDate: " + formattedTime); } catch (Exception e) { } } } image/是app1和app2使用的库目录。在这种情况下,我应该在app1和app2下为math/设置相同的设置吗?当然我知道它有效,但有没有更聪明的方法来设置公共库?

CmakeLists.txt

Roots |-- CMakeLists.txt |-- app1 | |-- CMakeLists.txt | `-- main.cc |-- app2 | |-- CMakeLists.txt | `-- main.cc |-- image | |-- CMakeLists.txt | |-- include | | `-- image_func.h | `-- src | `-- image_func.cc `-- math |-- CMakeLists.txt |-- include | `-- math_util.h `-- src `-- math_util.cc 位于下方。是否可以为app1和app2设置数学和图像参数?我的实际项目有很多使用多个库的应用程序。

CMakelists.txt

1 个答案:

答案 0 :(得分:2)

使用较新版本的CMake(自2.8.12开始),您可以使用target_link_libraries和相关功能来管理依赖项。通过指定PUBLIC,包含和库也可以使用库应用于所有目标。 这将减少重复工作。

对于数学和图像,您需要指定使用相应的包含目录以及您可能需要的任何库。

数学/的CMakeLists.txt

add_library(math ...)
target_include_directories(math PUBLIC    include ...)
target_link_libraries(math PUBLIC ...)

图像/的CMakeLists.txt

add_library(image ...)
target_include_directories(image PUBLIC include ...)
target_link_libraries(image PUBLIC ...)

APP1 /的CMakeLists.txt

add_executabke(app1 ...)
target_link_libraries(app1 PUBLIC image math)

APP2 /的CMakeLists.txt

add_executabke(app2 ...)
target_link_libraries(app2 PUBLIC image math)