CMake如果名称匹配则尝试将目录链接为可执行文件 [英] CMake trying to link directory as an executable if their names match

查看:94
本文介绍了CMake如果名称匹配则尝试将目录链接为可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在满足以下条件的CMake中遇到一个令人讨厌的错误:

I'm encountering an annoying error in CMake where the following conditions hold:


  1. 目标名称与目录名称相同

  2. 我正在使用 RUNTIME_OUTPUT_DIRECTORY 属性作为目标。

  1. A target name is identical to name of the directory defining the target.
  2. I am using the RUNTIME_OUTPUT_DIRECTORY property for the target.

在这些情况下,我得到了错误:

Under these conditions, I get the errors:


  • 链接CXX可执行文件。

  • / usr / bin / ld:无法打开输出文件。:是目录

  • Linking CXX executable .
  • /usr/bin/ld: cannot open output file .: Is a directory

CMake似乎试图建立一个名为的目标。,显然是试图

CMake seems to be trying to build a target named . , apparently trying to refer to the current directory name rather than the desired target name.

这是一个简单的示例。我的文件树是:

Here's a trivial example. My file tree is:

/tmp/example$ tree
.
├── build
└── src
    ├── CMakeLists.txt
    └── hello_world
        ├── CMakeLists.txt
        └── HelloWorld.cpp

src / CMakeLists.txt:

src/CMakeLists.txt:

set (ARBITRARY_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(hello_world)

src / hello_world:

src/hello_world:

add_executable(hello_world HelloWorld.cpp)
set_property(TARGET hello_world PROPERTY RUNTIME_OUTPUT_DIRECTORY ${ARBITRARY_OUTPUT_DIR})

...和 HelloWorld.cpp 本身是一个普通的Hello World程序,具有 main()方法。

...and HelloWorld.cpp itself is a trivial Hello World program, with a main() method.

我运行:

/tmp/example/build$ cmake ../src/ -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc-4.8 -DCMAKE_CXX_COMPILER=g++-4.8 -G"Unix Makefiles"
/tmp/example/build$ make VERBOSE=1

然后我得到:

[ 50%] Building CXX object hello_world/CMakeFiles/hello_world.dir/HelloWorld.o
cd /tmp/example/build/hello_world && /usr/bin/g++-4.8     -O3 -DNDEBUG   -o CMakeFiles/hello_world.dir/HelloWorld.o -c /tmp/example/src/hello_world/HelloWorld.cpp
[100%] Linking CXX executable .
cd /tmp/example/build/hello_world && /usr/bin/cmake -E cmake_link_script CMakeFiles/hello_world.dir/link.txt --verbose=1
/usr/bin/g++-4.8   -O3 -DNDEBUG   CMakeFiles/hello_world.dir/HelloWorld.o  -o . -rdynamic
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status

如您所见, CMakeFiles / hello_world.dir / link.txt 将链接目标设置为 -o。,显然不起作用。

As you can see, CMakeFiles/hello_world.dir/link.txt sets the linking target as -o . , which obviously won't work.

是这是CMake中的错误,还是我做错了什么?有解决方法吗?

我的工具是:


  • cmake版本3.5.1

  • Ubuntu 16.04 LTS(Xenial Xerus)

  • g ++-4.8

推荐答案

由于命令 add_subdirectory(hello_world),您的顶级构建目录包含 hello_world 目录)

Your top-level build directory contains hello_world directory because of command add_subdirectory(hello_world).

通过将 RUNTIME_OUTPUT_DIRECTORY 属性设置为顶级构建目录在此处创建名称为 hello_world 的可执行文件。

By setting RUNTIME_OUTPUT_DIRECTORY property to top-level build directory, you want to create executable file with name hello_world there.

但是在单个目录中,不可能同时具有相同的文件和目录名称

But in single directory it is impossible to have both file and directory with the same name.

您需要重命名子目录或可执行文件,或更改目录以放置可执行文件。

You need to rename either subdirectory or executable or change directory to place executable.

这篇关于CMake如果名称匹配则尝试将目录链接为可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆