CMake 无法使用 C++ 确定链接器语言

2021-12-26 00:00:00 c cmake c++

我正在尝试使用 Visual Studio 2010 和 Cygwin 在 Windows 7 x64 上运行 cmake hello world 程序,但似乎两者都无法工作.我的目录结构如下:

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/

我做了一个 cd build 后跟一个 cmake ..,然后得到一个错误说明

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

但是,如果我在我的文件系统和 src/CMakeLists.txt 中将 main.cpp 的扩展名更改为 main.c,一切都会按预期进行.这是从 Visual Studio 命令提示符(Visual Studio 解决方案生成器)和 Cygwin 终端(Unix Makefiles 生成器)运行的情况.

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

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

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

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

相关文章