【问题标题】:Add files with CMake使用 CMake 添加文件
【发布时间】:2012-09-11 17:52:19
【问题描述】:

我现在正在使用 CMake 构建一个动态库,我现在遇到的问题与为库添加文件有关。我的项目结构如下:

----src
     |
  thrid_party---boost_1_50_0----boost
     |       ---   |        ----libs
     |       ---   |        ---- | --- filesystem
     |       ---   |        ---- | ---   |  ------src

我的CMakeLists.txt文件位于src目录下,文件内容如下:

cmake_minimum_required( VERSION 2.6 )
project (temp)
#build the third party library
include_directories( ${irisimp_SOURCE_DIR}/../third_party/boost_1_50_0)
set (boost_path 
${irisimp_SOURCE_DIR}/../third_party/boost_1_50_0/libs)

set (BOOST_LIST
${boost_path}/filesystem/src/codecvt_error_category.cpp 
${boost_path}/filesystem/src/operations.cpp 
${boost_path}/filesystem/src/path.cpp 
${boost_path}/filesystem/src/path_traits.cpp 
${boost_path}/filesystem/src/portability.cpp 
${boost_path}/filesystem/src/unique_path.cpp 
${boost_path}/filesystem/src/utf8_codecvt_facet.cpp 
${boost_path}/filesystem/src/windows_file_codecvt.cpp 
${boost_path}/filesystem/src/windows_file_codecvt.hpp 
)
add_library ( boost SHARED ${BOOST_LIST})

我运行这个脚本没有问题,但是,输出的 Visual Studio 10 项目不包含${boost_path}/filesystem/src文件夹中的所有源文件。实际上只保留 windows_file_codecvt.cpp。因此,编译此项目将失败。我想知道我应该怎么做才能确保 Visual Studio 10 项目可以包含 CMakeLists.txt 中指示的所有源文件。谢谢!

【问题讨论】:

  • 一切看起来都很好(除了结构图中的错字“thrid”)。我假设列出的 Boost.Filesystem 文件都实际存在于您指定的位置。您可以在设置BOOST_LIST 后尝试输出其内容:message("BOOST_LIST: ${BOOST_LIST}"),或者您可以获取boost 目标get_target_property(BoostSources boost SOURCES) 的源并输出:message("BoostSources: ${BoostSources}") 以查看它是否对问题。
  • @Fraser 谢谢你的建议,看来只是在我的电脑上失败了,如果在另一台电脑上运行同样的脚本,那就没问题了。应该和我电脑的设置有关吧。

标签: c++ visual-c++ cmake


【解决方案1】:

尝试将分号放在路径之间,如下所示:

set (BOOST_LIST
  ${boost_path}/filesystem/src/codecvt_error_category.cpp;
  ${boost_path}/filesystem/src/operations.cpp;
  ${boost_path}/filesystem/src/path.cpp;
  ${boost_path}/filesystem/src/path_traits.cpp; 
  ${boost_path}/filesystem/src/portability.cpp;
  ${boost_path}/filesystem/src/unique_path.cpp;
  ${boost_path}/filesystem/src/utf8_codecvt_facet.cpp; 
  ${boost_path}/filesystem/src/windows_file_codecvt.cpp; 
  ${boost_path}/filesystem/src/windows_file_codecvt.hpp;
)

如果这不起作用,请尝试

add_library(boost SHARED
  ${boost_path}/filesystem/src/codecvt_error_category.cpp
  ${boost_path}/filesystem/src/operations.cpp 
  ${boost_path}/filesystem/src/path.cpp 
  ${boost_path}/filesystem/src/path_traits.cpp
  ...
  ${boost_path}/filesystem/src/windows_file_codecvt.hpp)

这可能有助于调试问题所在。

【讨论】:

  • 这本来是一条评论,但 SO 认为我还没有配得上 cmets。
  • 嗯,奇怪。 BOOST_LIST 无论如何都应该在列表中。
  • 这很奇怪。也许它可能与换行符而不是/没有空格有关。来自#ogre3d 的@arrowdodger?真的在这里。
猜你喜欢
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 2010-10-27
  • 2014-07-12
相关资源
最近更新 更多