CMake 无法使用 C++ 确定链接器语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11801186/
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
CMake unable to determine linker language with C++
提问by Chris Covert
I'm attempting to run a cmake hello world program on Windows 7 x64 with both Visual Studio 2010 and Cygwin, but can't seem to get either to work. My directory structure is as follows:
我正在尝试使用 Visual Studio 2010 和 Cygwin 在 Windows 7 x64 上运行 cmake hello world 程序,但似乎两者都无法工作。我的目录结构如下:
HelloWorld
-- CMakeLists.txt
-- src/
-- -- CMakeLists.txt
-- -- main.cpp
-- build/
I do a cd build
followed by a cmake ..
, and get an error stating that
我做了 acd build
后跟 a cmake ..
,并得到一个错误说明
CMake Error: CMake can not determine linker language for target:helloworld
CMake Error: Cannot determine link language for target "helloworld".
However, if I change the extension of main.cpp to main.c both on my filsystem and in src/CMakeLists.txt
everything works as expected. This is the case running from both the Visual Studio Command Prompt (Visual Studio Solution Generator) and the Cygwin Terminal (Unix Makefiles Generator).
但是,如果我将 main.cpp 的扩展名更改为 main.c 在我的文件系统和src/CMakeLists.txt
一切都按预期工作。这是从 Visual Studio 命令提示符(Visual Studio 解决方案生成器)和 Cygwin 终端(Unix Makefiles Generator)运行的情况。
Any idea why this code wouldn't work?
知道为什么这段代码不起作用吗?
CMakeLists.txt
CMakeLists.txt
PROJECT(HelloWorld C)
cmake_minimum_required(VERSION 2.8)
# include the cmake modules directory
set(CMAKE_MODULE_PATH ${HelloWorld_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
add_subdirectory(src)
src/CMakeLists.txt
源代码/CMakeLists.txt
# Include the directory itself as a path to include directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Create a variable called helloworld_SOURCES containing all .cpp files:
set(HelloWorld_SOURCES main.cpp)
# Create an executable file called helloworld from sources:
add_executable(hello ${HelloWorld_SOURCES })
src/main.cpp
src/main.cpp
int main()
{
return 0;
}
采纳答案by olovb
Try changing
尝试改变
PROJECT(HelloWorld C)
into
进入
PROJECT(HelloWorld C CXX)
or just
要不就
PROJECT(HelloWorld)
See: http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:project
请参阅:http: //www.cmake.org/cmake/help/v2.8.8/cmake.html#command: project
回答by Joakim
I also got the error you mention:
我也收到了你提到的错误:
CMake Error: CMake can not determine linker language for target:helloworld
CMake Error: Cannot determine link language for target "helloworld".
In my case this was due to having C++ files with the .cc
extension.
在我的情况下,这是由于具有.cc
扩展名的C++ 文件。
If CMake is unable to determine the language of the code correctly you can use the following:
如果 CMake 无法正确确定代码的语言,您可以使用以下方法:
set_target_properties(hello PROPERTIES LINKER_LANGUAGE CXX)
The accepted answer that suggests appending the language to the project()
statement simply adds more strict checking for what language is used (according to the documentation), but it wasn't helpful to me:
已接受的答案建议将语言附加到project()
语句中,只是对使用的语言进行了更严格的检查(根据文档),但这对我没有帮助:
Optionally you can specify which languages your project supports. Example languages are CXX (i.e. C++), C, Fortran, etc. By default C and CXX are enabled. E.g. if you do not have a C++ compiler, you can disable the check for it by explicitly listing the languages you want to support, e.g. C. By using the special language "NONE" all checks for any language can be disabled. If a variable exists called CMAKE_PROJECT__INCLUDE_FILE, the file pointed to by that variable will be included as the last step of the project command.
您可以选择指定您的项目支持哪些语言。示例语言有 CXX(即 C++)、C、Fortran 等。默认情况下启用 C 和 CXX。例如,如果您没有 C++ 编译器,您可以通过明确列出您想要支持的语言(例如 C)来禁用对它的检查。通过使用特殊语言“NONE”可以禁用对任何语言的所有检查。如果存在名为 CMAKE_PROJECT__INCLUDE_FILE 的变量,则该变量指向的文件将作为项目命令的最后一步包含在内。
回答by Moebius
In my case, it was just because there were no source file in the target. All of my library was template with source code in the header. Adding an empty file.cpp solved the problem.
就我而言,这只是因为目标中没有源文件。我的所有库都是模板,标题中包含源代码。添加一个空的 file.cpp 解决了这个问题。
回答by Jolly Roger
Confusing as it might be, the error also happens when a cpp file included in the project does not exist.
尽管可能令人困惑,但当项目中包含的 cpp 文件不存在时,也会发生该错误。
If you list your source files in CMakeLists.txt and mistakenly type a file name then you get this error.
如果您在 CMakeLists.txt 中列出源文件并错误地键入文件名,则会出现此错误。
回答by f_i
A bit unrelated answer to OP but for people like me with a somewhat similar problem.
对 OP 的回答有点不相关,但对于像我这样有类似问题的人来说。
Use Case: Ubuntu (C, Clion, Auto-completion):
用例:Ubuntu(C、Clion、自动完成):
I had the same error,
我有同样的错误,
CMake Error: Cannot determine link language for target "hello".
CMake 错误:无法确定目标“hello”的链接语言。
set_target_properties(hello PROPERTIES LINKER_LANGUAGE C)
help fixes that problem but the headers aren't included to the project and the autocompletion wont work.
set_target_properties(hello PROPERTIES LINKER_LANGUAGE C)
帮助解决了该问题,但标题未包含在项目中,并且自动完成功能不起作用。
This is what i had
这就是我所拥有的
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES ./)
add_executable(hello ${SOURCE_FILES})
set_target_properties(hello PROPERTIES LINKER_LANGUAGE C)
No errors but not what i needed, i realized including a single file as source will get me autocompletion as well as it will set the linker to C
.
没有错误但不是我需要的,我意识到将单个文件作为源文件将使我自动完成并将链接器设置为C
.
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES ./1_helloworld.c)
add_executable(hello ${SOURCE_FILES})
回答by user2999709
I also faced a similar error while compiling my C-based code. I fixed the issue by correcting the source file path in my cmake
file. Please check the source file path of each source file mentioned in your cmake
file. This might help you too.
在编译基于 C 的代码时,我也遇到了类似的错误。我通过更正文件中的源文件路径解决了该问题cmake
。请检查您的cmake
文件中提到的每个源文件的源文件路径。这也可能对你有帮助。
回答by HimalayanCoder
By default the JNI Native folder is named as jni. Renaming it to cppfixed the issue
默认情况下,JNI Native 文件夹被命名为jni。将其重命名为cpp解决了问题
回答by AKJ
I managed to solve mine, by changing
我设法解决了我的问题,通过改变
add_executable(file1.cpp)
to
到
add_executable(ProjectName file1.cpp)
回答by adem
In my case, implementing a member function of a class in a header file cause this error. Separating interface (in x.h file) and implementation (in x.cpp file) solves the problem.
就我而言,在头文件中实现类的成员函数会导致此错误。分离接口(在 xh 文件中)和实现(在 x.cpp 文件中)解决了这个问题。