C++ CMake 错误:CMake 无法确定目标的链接器语言:myapp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31130612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:51:50  来源:igfitidea点击:

CMake Error: CMake can not determine linker language for target: myapp

c++cmake

提问by Ahsan Roy

I am trying to compile vMime by cmake, but I am getting error above, I am using graphical interface of cmake and my makefiles.txt is below. It configures properly but do not generate

我正在尝试通过 cmake 编译 vMime,但出现上面的错误,我正在使用 cmake 的图形界面,我的 makefiles.txt 如下。它配置正确但不生成

cmake_minimum_required(VERSION 2.8)
PROJECT(CXX)#vmime
enable_language(CXX)
set(VerifyCXX VerifyCXX.cxx)
add_definitions(-DVERIFY_CXX)
set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE Cxx)
add_executable(myapp vmime)
install(TARGETS myapp DESTINATION bin)

Help will be highly appreciated as I am stuck at point for couple of days.

帮助将受到高度赞赏,因为我在这一点上停留了几天。

回答by Peter

CMake probably can not determine linker language for target myapp, because the target does not contain any source files with recognized extensions.

CMake 可能无法确定 target 的链接器语言myapp,因为 target 不包含任何具有可识别扩展名的源文件。

add_executable(myapp vmime)

should be probably replaced by

可能应该替换为

add_executable(myapp ${VerifyCXX})

Also this command

还有这个命令

set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE Cxx) 

cannot be succesfull, because ${TARGET}is used-before-set. You should call it after add_executable

不能成功,因为${TARGET}在设置之前使用。你应该在之后调用它add_executable

set_target_properties(myapp PROPERTIES LINKER_LANGUAGE CXX)

Note that usually it is not needed at all.

请注意,通常根本不需要它。

回答by sage

For others' benefit, make sure you did not overlook an earlier error such as:

为了他人的利益,请确保您没有忽略之前的错误,例如:

Cannot find source file:

    MyFirstSourceFile.cpp
Cannot find source file:

    MyFirstSourceFile.cpp

Another way to cause CMake to give you the error, "CMake Error: CMake can not determine linker language for target: myapp", is if you mistakenly point it exclusively at sources that do not exist.

导致 CMake 向您提供错误“CMake 错误:CMake 无法确定目标:myapp 的链接器语言”的另一种方法是,如果您错误地将其专门指向不存在的源。

For instance: I'm moving files from one directory to another and had the pre-move files with the post-move paths in my CMakeLists.txt. My output window is not very tall and I focused too soon on the "can not determine linker language" error!

例如:我正在将文件从一个目录移动到另一个目录,并且在我的 CMakeLists.txt 中有带有移动后路径的移动前文件。我的输出窗口不是很高,而且我过早地将注意力集中在“无法确定链接器语言”错误上!

回答by Jayhello

I use clion IDE based on cmake, my source files named *.cc

我使用基于 cmake 的 clion IDE,我的源文件名为 *.cc

project(server)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
file(GLOB SRC_FILE "main.cc" "listenfd.cc" "socket_util.cc"
    "timers.cc" "network.cc" "main_event.cc")
add_executable(server ${server})
set_target_properties(server PROPERTIES LINKER_LANGUAGE CXX)

after I change

在我改变之后

add_executable(server ${server}) to 
add_executable(server "main.cc")

then I solved it, I really don't know why? after experiment I found when use file(GLOB ....) like file(GLOB "src/main.cc") I must specify the relative path, then it works.

然后我解决了,我真的不知道为什么?经过实验,我发现当使用 file(GLOB ....) like file(GLOB "src/main.cc") 时,我必须指定相对路径,然后它才能工作。