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

但是,如果我在我的 filsystem 和 src/CMakeLists.txt 中将 main.cpp 的扩展名更改为 main.c,一切都会按预期工作。这是从 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;
}

原文由 Chris Covert 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

尝试改变

PROJECT(HelloWorld C)

进入

PROJECT(HelloWorld C CXX)

要不就

PROJECT(HelloWorld)

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

原文由 olovb 发布,翻译遵循 CC BY-SA 4.0 许可协议

只需检查源文件的路径。 (到各自的 cpp)

原文由 Rahul Das 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题