154

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;
}
asked Aug 3, 2012 at 18:19
3
  • 1
    "[...]if I change the extension of main.cpp[...]" What do you change it to? .cc? Commented Aug 3, 2012 at 18:21
  • 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. Commented Aug 3, 2012 at 18:33
  • Change PROJECT(HelloWorld C) to PROJECT(HelloWorld CXX). C and C++ are different languages. Commented Mar 23, 2022 at 11:49

10 Answers 10

290

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.

tambre
4,8965 gold badges47 silver badges57 bronze badges
answered May 21, 2013 at 11:40
Sign up to request clarification or add additional context in comments.

3 Comments

In my case, my file had an .hpp extension. This resolved it!
Same for me, .hpp file and this fixed it.
Note: this only worked for me in the following order: project(<myProjName>) --> add_library(${PROJECT_NAME} <other settings go here> <then all my .h files went here) --> set_target_properties(${PROJECT_NAME} ...) as from the answer, and finally target_link_libraries(...). I first tried set_target_properties above add_library and it did not work.
118

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.

answered Apr 8, 2015 at 14:52

5 Comments

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
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.
It is as simple as that. Typo in the path lead to no *.cpp files in the sources. All fine after that. Thank you!
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().
64

Try changing

PROJECT(HelloWorld C)

into

PROJECT(HelloWorld C CXX)

or just

PROJECT(HelloWorld)

See: https://cmake.org/cmake/help/latest/command/project.html

starball
59.4k52 gold badges312 silver badges1k bronze badges
answered Aug 3, 2012 at 19:19

2 Comments

This was not helpful for me. The linker error remained, see my answer what helped to fix it.
Yes, it is correct. I have a main.cpp file, even it is a pure c source code, it should be using the PROJECT(HelloWorld CXX). Otherwise, it will generate the same error.
24

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.

answered Oct 18, 2020 at 16:48

1 Comment

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

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.

answered Apr 4, 2018 at 6:14

1 Comment

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

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})
answered Aug 1, 2019 at 14:59

1 Comment

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
2

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.

Robert Columbia
6,45115 gold badges34 silver badges42 bronze badges
answered Jun 20, 2018 at 9:46

Comments

2

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

answered Oct 14, 2021 at 4:26

Comments

0

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

answered Apr 25, 2018 at 10:29

Comments

-3

I managed to solve mine, by changing

add_executable(file1.cpp)

to

add_executable(ProjectName file1.cpp)
Zoe - Save the data dump
28.4k22 gold badges130 silver badges163 bronze badges
answered Oct 25, 2018 at 12:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.