Nice programing

CMake가 C ++로 링커 언어를 결정할 수 없습니다.

nicepro 2020. 11. 12. 20:14
반응형

CMake가 C ++로 링커 언어를 결정할 수 없습니다.


Visual Studio 2010과 Cygwin을 모두 사용하여 Windows 7 x64에서 cmake hello world 프로그램을 실행하려고하는데 둘 다 작동하지 않는 것 같습니다. 내 디렉토리 구조는 다음과 같습니다.

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

나는 cd build다음을 수행하고 다음 cmake ..과 같은 오류가 발생합니다.

CMake Error: CMake can not determine linker language for target:helloworld
CMake Error: Cannot determine link language for target "helloworld".

그러나 내 파일 시스템에서 main.cpp의 확장자를 main.c로 변경하면 src/CMakeLists.txt모든 것이 예상대로 작동합니다. Visual Studio 명령 프롬프트 (Visual Studio 솔루션 생성기) 및 Cygwin 터미널 (Unix Makefiles 생성기)에서 실행되는 경우입니다.

이 코드가 작동하지 않는 이유를 아십니까?

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

변경 시도

PROJECT(HelloWorld C)

으로

PROJECT(HelloWorld C CXX)

아니면 그냥

PROJECT(HelloWorld)

참조 : http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:project


또한 언급 한 오류가 있습니다.

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


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.


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.


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


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

I managed to solve mine, by changing

add_executable(file1.cpp)

to

add_executable(ProjectName file1.cpp)

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.

참고URL : https://stackoverflow.com/questions/11801186/cmake-unable-to-determine-linker-language-with-c

반응형