混合C和C ++中的CMake,什么CMakeCCompilerId.c和我怎么能抛弃它 [英] Mixing C and C++ in CMake, what CMakeCCompilerId.c and how can I discard it

查看:4861
本文介绍了混合C和C ++中的CMake,什么CMakeCCompilerId.c和我怎么能抛弃它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的项目,混合C和C ++(包装一个C lib放到一个C ++类,并使其易于在C ++项目中使用)。该项目是建立与VisualStudio中,我Linux或Mac下的很,所以我想将其转换在CMake的项目。

I have simple project that mixes C and C++ (wrapping a C lib into a C++ class and making it easy to use in C++ project). The project was build with VisualStudio and I'm under Linux or Mac so I'd like to convert it in a CMake project.

该VS项目内容充满了

<ClInclude Include="a_file.h" />

所以我认为该文件只是包含没有任何复杂的构建系统。这是我的CMakeLists.txt:

so I think the file are just included without any complex build system. Here is my CMakeLists.txt:

cmake_minimum_required(版本2.8)

cmake_minimum_required(VERSION 2.8)

# project
set(PROJECTNAME "Test")
project(${PROJECTNAME} C CXX)
set (CMAKE_CXX_FLAG "-Wall")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "./")

include_directories(${CMAKE_CURRENT_BINARY_DIR})

find_package(OpenCV REQUIRED)

file(GLOB_RECURSE ${PROJECTNAME}_HEADERS ${CMAKE_SOURCE_DIR}/*.h)
file(GLOB_RECURSE ${PROJECTNAME}_SOURCES ${CMAKE_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE ${PROJECTNAME}_SOURCES ${CMAKE_SOURCE_DIR}/*.c)

include_directories (include)
include_directories (src)

SET(${PROJECTNAME}_SOURCES
    main.cpp
    ${${PROJECTNAME}_SOURCES}
)


ADD_EXECUTABLE(${PROJECTNAME}
    ${${PROJECTNAME}_SOURCES}
    ${${PROJECTNAME}_HEADERS}

)

我知道这是不是一个很好的规则可循,但我只需要使用此code一个快速和肮脏的方式。

I know that is not a good rule to follow, but I just need a quick and dirty way to use this code.

现在,如果我做这个测试的main.cpp:

Now, if I make with this test main.cpp:

#include <iostream>
int main(int argc, char *argv[]) {
    std::cout << "main > ." << std::endl;
    return 0;
}

我得到的复制符号错误CMakeCCompilerId.c

I get a duplicate symbol error for CMakeCCompilerId.c

duplicate symbol _main in:
    CMakeFiles/Test.dir/main.cpp.o
    CMakeFiles/Test.dir/CMakeFiles/2.8.12.2/CompilerIdC/CMakeCCompilerId.c.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

据我得到这个CMakeCCompilerId.c文件是什么,CMake的添加处理用C ...

As far as I get this CMakeCCompilerId.c file is something that cmake add for dealing with C...

所以,在这里我的问题:什么是增值的CMake-AUTO CMakeCCompilerId.c?我怎么能编译包含机器人C和C ++?

So, here my question: what is the cmake-auto-added CMakeCCompilerId.c ? How can I compile a project with a main function that contains bot C and C++?

我解决了这个问题只是用

I resolved the problem just using

project(${PROJECTNAME} CXX)

而不是

project(${PROJECTNAME} C CXX)

不过我很好奇,反正去了解它,并CMake的混合如何在两种语言

But I'm curious anyway to understand it and how CMake mixes the two languages

推荐答案

文件(GLOB_RECURSE $ {PROJECTNAME} _SOURCES $ {CMAKE_SOURCE_DIR} / *。C)可能您的问题的原因。

The file(GLOB_RECURSE ${PROJECTNAME}_SOURCES ${CMAKE_SOURCE_DIR}/*.c) might be the reason of your issue.

CMake的运行编译器的一些检查,这样做,它会产生一些.c文件,并在构建目录保存它们。

CMake runs some checks on the compiler, and to do so, it generates some .c files and saves them in the build directory

如果build目录是主要的一个子目录,第二次是在 GLOB_RECURSE 运行,它将在构建目录下降,因此将增加这些cmake的在$ {} PROJECTNAME文件_SOURCES变量。

If the build directory is a subdirectory of the main one, the second time that the GLOB_RECURSE runs, it will descend in the build directory, and therefore will add these cmake files in the ${PROJECTNAME}_SOURCES variable.

如果你的源代码都在src目录,你的头的头目录下,你可以使用

If your sources are all in the "src" directory, and your headers in the "header" directory, you can use

file(GLOB_RECURSE ${PROJECTNAME}_HEADERS ${CMAKE_SOURCE_DIR}/include/*.h)
file(GLOB_RECURSE ${PROJECTNAME}_SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE ${PROJECTNAME}_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c)

这应该解决这个问题,因为它不会尝试在生成目录递归。

And this should fix the issue, because it will not try to recurse in the build directory.

这篇关于混合C和C ++中的CMake,什么CMakeCCompilerId.c和我怎么能抛弃它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆