C++ CMake 无法确定目标的链接器语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16655705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:31:04  来源:igfitidea点击:

CMake cannot determine linker language for target

c++cmake

提问by Syntactic Fructose

To start off, I've taken a look at thispost and couldn't find a solution to my problem. I'm attempting to set up a library in a folder using two header files and link with my main program, in my folder containerit includes:

首先,我查看了这篇文章,但找不到解决我的问题的方法。我正在尝试使用两个头文件在文件夹中设置一个库并与我的主程序链接,在我的文件夹容器中,它包括:

linkedStack.h
linkedQueue.h

the CMakeLists.txt in my container folder is

我的容器文件夹中的 CMakeLists.txt 是

add_library(container linkedQueue.h linkedStack.h)

install (TARGETS container DESTINATION bin)
install (FILES linkedQueue.h linkedStack.h DESTINATION include)

while my CMakeLists.txt in the source directory is:

而我在源目录中的 CMakeLists.txt 是:

cmake_minimum_required(VERSION 2.6)
project(wordLadder)

# set version number
set (MAJOR 1)
set (MINOR 0)

# configure header file to be placed in binary
configure_file(
        "${PROJECT_SOURCE_DIR}/ladderConfig.h.in"
        "${PROJECT_BINARY_DIR}/ladderConfig.h"
)

# add binary tree to search path for include files
# so we can find config
include_directories("${PROJECT_BINARY_DIR}")

#add container library
include_directories ("${PROJECT_SOURCE_DIR}/container")
add_subdirectory(container)

#add executable
add_executable(wordLadder ladderMain.cpp)
target_link_libraries (wordLadder container)

install (TARGETS wordLadder DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/ladderConfig.h"
         DESTINATION include)

and the error I get:

和我得到的错误:

CMake Error: Cannot determine link language for target "container".
CMake Error: CMake can not determine linker language for target:container
-- Generating done
-- Build files have been written to: /home/gmercer/Linux_dev/wordLadder/build

I'm not sure what i'm doing wrong here, but I think it has something to do with my library CMake file.

我不确定我在这里做错了什么,但我认为这与我的库 CMake 文件有关。

回答by Nikolay Viskov

You have added target for creating containerlibrary. That target contains only header files. See CMake documentation

您已添加创建container库的目标。该目标仅包含头文件。请参阅CMake 文档

add_library: Add a library to the project using the specified source files.

add_library( [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] source1 source2 ... sourceN)

Adds a library target called to be built from the source files listed in the command invocation. The corresponds to the logical target name and must be globally unique within a project. The actual file name of the library built is constructed based on conventions of the native platform (such as lib.a or .lib).

add_library:使用指定的源文件向项目添加库。

add_library( [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] source1 source2 ... sourceN)

添加要从命令调用中列出的源文件构建的库目标。对应于逻辑目标名称,并且在项目中必须是全局唯一的。构建的库的实际文件名是根据本机平台的约定(例如 lib.a 或 .lib)构建的。

But you can not build library just from header files without any cpp file. That's why you got such error.

但是你不能只从没有任何 cpp 文件的头文件构建库。这就是为什么你得到这样的错误。