ChatGPT解决这个技术问题 Extra ChatGPT

CMake unable to determine linker language with C++

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:

HelloWorld
-- CMakeLists.txt
-- src/
-- -- CMakeLists.txt
-- -- main.cpp
-- build/

I do a cd build followed by a cmake .., and get an error stating that

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).

Any idea why this code wouldn't work?

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

# 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

int main()
{
  return 0;
}
"[...]if I change the extension of main.cpp[...]" What do you change it to? .cc?
oops. Left that out by accident. I change it to '.c'. Edited in the original post. It almost makes me think that there isn't a cpp compiler or something of the sort, but g++ is insalled and visual studio shouldn't have problems with c++ either.
Change PROJECT(HelloWorld C) to PROJECT(HelloWorld CXX). C and C++ are different languages.

t
tambre

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.

If CMake is unable to determine the language of the code correctly you can use the following:

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:

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.


In my case, my file had an .hpp extension. This resolved it!
Same for me, .hpp file and this fixed it.
M
Moebius

In my case, it was just because there were no source file in the target. All of my code was a template with the source code in the header file. Adding an empty file.cpp solved the problem.


set target properties also works for the no cpp file issue.
Kudos for the tip. I also did forget to move my sources to the respective src subdirectory of my newly created cmake project (a shared library) and this was basically the cause of the whole problem. In such cases one really does appreciate having a wizard to take care of the structure of your cmake project. :D
Same reason here (copy-paste mistake). Thanks!
Useful tip. Even if your "library" is header only, you should create one .cpp file that does a #include for each file. Even though there will be no output when your library is compiled, it will do syntax checking of your file, and also check for header dependencies (for example system headers) that you may have missed.
I think the notionally correct way to go about this is to configure your header-only library as an INTERFACE library and set the interface include directory on that target to your header location. Then when you want to use it in an application (e.g. your test applications, which will be executable targets) you 'link' against it. This has the advantage of making installation trivial, and CMake will even generate exported targets for you which can be found by other projects with find_package().
o
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


This was not helpful for me. The linker error remained, see my answer what helped to fix it.
J
Jolly Roger

Confusing as it might be, the error also happens when a cpp file included in the project does not exist.

If you list your source files in CMakeLists.txt and mistakenly type a file name then you get this error.


This works as its own answer as it is independent of what the other answers said. Also this fixed my issue.
z
zhm

I want to add another solution in case a library without any source files shall be build. Such libraries are also known as header only libraries. By default add_library expects at least one source file added or otherwise the mentioned error occurs. Since header only libraries are quite common, cmake has the INTERFACE keyword to build such libraries. The INTERFACE keyword is used as shown below and it eliminates the need for empty source files added to the library.

add_library(myLibrary INTERFACE)
target_include_directories(myLibrary INTERFACE {CMAKE_CURRENT_SOURCE_DIR})

The example above would build a header only library including all header files in the same directory as the CMakeLists.txt. Replace {CMAKE_CURRENT_SOURCE_DIR} with a path in case your header files are in a different directory than the CMakeLists.txt file.

Have a look at this blog post or the cmake documentation for further info regarding header only libraries and cmake.


I'd say this is the proper way to do it.
f
f_i

A bit unrelated answer to OP but for people like me with a somewhat similar problem.

Use Case: Ubuntu (C, Clion, Auto-completion):

I had the same error,

CMake Error: Cannot determine link language for target "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.

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.

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})

Just noticed that you're using CXX_FLAGS to set the C++ standard version, and thought I'd mention the CXX_STANDARD variable, which I think is the recommended way cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html and should be available in cmake 3.5
R
Robert Columbia

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.


R
Rahul Das

Simply check the path to source file. (to the respective cpp)


H
HimalayanCoder

By default the JNI Native folder is named as jni . Renaming it to cpp fixed the issue


Z
Zoe stands with Ukraine

I managed to solve mine, by changing

add_executable(file1.cpp)

to

add_executable(ProjectName file1.cpp)

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now