CMake unable to determine linker language with C++

后端 未结 10 1644
野趣味
野趣味 2020-11-28 06:41

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

相关标签:
10条回答
  • 2020-11-28 07:28

    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})
    
    0 讨论(0)
  • 2020-11-28 07:32

    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.

    0 讨论(0)
  • 2020-11-28 07:33

    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.

    0 讨论(0)
  • 2020-11-28 07:33

    I managed to solve mine, by changing

    add_executable(file1.cpp)
    

    to

    add_executable(ProjectName file1.cpp)
    
    0 讨论(0)
提交回复
热议问题