CMake不会将C库链接到C ++程序 [英] CMake won't Link C library to C++ program

查看:88
本文介绍了CMake不会将C库链接到C ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是我能想到的最短的工作示例:

Probably the shortest working example I can think of:

CMakeLists.txt

project(myprogs)
cmake_minimum_required(VERSION 2.8)

add_executable(myprog2 main.c)
add_executable(myprog main.cpp)
add_library(mylib SHARED mylib.c)

target_link_libraries(myprog2 mylib)
target_link_libraries(myprog mylib)

main.c / main.cpp (相同内容):

main.c/main.cpp (identical contents):

#include "mylib.h"

int main(int argc, char** argv)
{
  doit();
}

mylib.h

#ifndef MYLIB_H
#define MYLIB_H

void doit(void);

#endif

mylib.c

#include "mylib.h"
#include <stdio.h>

void doit(void)
{
  printf("doit");
}

系统:


  • Ubunto 15.10

  • gcc 5.2.1 / clang 3.6.2(都尝试过)

  • CMake 3.2.2

当我执行 make myprog 时, myprog 的链接阶段抱怨没有对 doit 的引用。但是,如果我使用 make myprog2 ,则所有内容都将正确链接,程序将按预期运行。

When I do a make myprog, myprog's link phase complains that there is an undefined reference to doit. However, if I use make myprog2, everything links correctly and the program runs as expected.

我不会了解为什么CMake无法在C ++程序中正确正确链接到 mylib 。从编译器得到详细的输出(我已经修剪了一些到系统库路径/目标文件的链接):

I don't understand why CMake isn't properly linking to mylib correctly in the C++ program. Getting verbose output form the compiler gives (I've trimmed some of the linking to system library paths/object files):


/ usr / bin / ld -export-dynamic --eh-frame-hdr -m elf_x86_64 -dyna
mic-linker /lib64/ld-linux-x86-64.so.2 -o myprog CMakeFiles / myprog.dir /main.cpp.o libmylib.so -rpath / home / andrew / code / misc / myprog / build -lstdc ++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc

"/usr/bin/ld" -export-dynamic --eh-frame-hdr -m elf_x86_64 -dyna mic-linker /lib64/ld-linux-x86-64.so.2 -o myprog CMakeFiles/myprog.dir/main.cpp.o libmylib.so -rpath /home/andrew/code/misc/myprog/build -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc

奇怪的是,它没有使用 -lmylib 与mylib链接。对于 myprog2 ,我得到类似的输出。

Strangely, it's not using a -lmylib to link with mylib. I get a similar output for myprog2.

我的问题是为什么会这样,更重要的是,怎么办我得到 myprog 正确链接到 mylib

My question is why is this happening, and more importantly, how do I get myprog to properly link to mylib?

推荐答案

您需要声明 c 在c ++中具有外部 C 的功能。 c ++ 的问题的编译器会在为了允许函数重载,例如

You need to declare c functions with extern "C" in c++. The c++ compiler changes function names in order to allow function overloading, so for instance

int function(int value);

int function(char *value);

两者都可以在的问题,编译器将生成两个具有不同名称的不同函数,以使其正常工作。

both can be defined in c++ with exactly the same name, the compiler will generate two different functions with different names for this to work correctly.

,您将无法执行此操作,并且无需修改函数名称。通过使用 extern C ,可以防止编译器更改函数名,因此链接阶段将按预期工作。

In c you can't do this, and the function name will not need to be modified. By using extern "C" you prevent the compiler from altering function names, and so the link phase will work as you expect it.

要解决此问题,请按以下方式启动main.cpp:

To fix it, start main.cpp this way:

extern "C" {
#include "mylib.h"
}

这篇关于CMake不会将C库链接到C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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