强制target_link_libraries使用c ++链接器 [英] force target_link_libraries to use c++ linker

查看:2343
本文介绍了强制target_link_libraries使用c ++链接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将静态c ++库链接到ac可执行文件时,是否有强制cmake使用c ++链接器的方法?

Is there a way to force cmake to use the c++ linker when linking a static c++ library to a c executable?

我有一个由两个对象组成的静态库,一个C ++文件和C文件中的函数的C包装(构造函数,析构函数和打印函数),类似于 this SO回答。最后一段说明:

I have a static library that consists of 2 objects, a C++ file and a C wrapper to the functions in that file (Constructor, Destructor, and Print function), similar to this SO answer. The last paragraph states:


有趣的部分现在确保您获得所有需要的C ++库链接到你的大型库正确。对于gcc(或clang),这意味着只使用g ++做最后的链接阶段。

The fun part is now ensuring that you get all the required C++ libraries linked into you larger library correctly. For gcc (or clang) that means just doing the final link stage using g++.

我可以使用我的MCVE验证。将 gcc 替换为 g ++ 可修复问题和一切正常

I can verify this with my MCVE. Replacing gcc with g++ fixes the problem and everything works

$ gcc -static main.c -L. -lCPPclass -o main
./libCPPclass.a(CInt.o): In function `newCINT':
CInt.cpp:(.text+0xd): undefined reference to `operator new(unsigned long)'
CInt.cpp:(.text+0x28): undefined reference to `operator delete(void*)'
./libCPPclass.a(CInt.o): In function `delCINT':
CInt.cpp:(.text+0x5e): undefined reference to `operator delete(void*)'
./libCPPclass.a(CInt.o):(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
./libCPPclass.a(CPPclass.o): In function `CPPclass::print_success()':
CPPclass.cpp:(.text+0x26): undefined reference to `std::cout'
CPPclass.cpp:(.text+0x2b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
./libCPPclass.a(CPPclass.o): In function `__static_initialization_and_destruction_0(int, int)':
CPPclass.cpp:(.text+0x54): undefined reference to `std::ios_base::Init::Init()'
CPPclass.cpp:(.text+0x63): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
$
$#REPLACE gcc with g++
$
$ g++ -static main.c -L. -lCPPclass -o main
$ ./main
Success!

然而,我的实际代码是使用CMake构建的,所以我试图得到这个MCVE与CMake,这让我回到我原来的问题。我的CMakeLists.txt文件如下:

My real code, however, is built using CMake, so I'm trying to get this MCVE built with CMake, which takes me back to my original question. My CMakeLists.txt file is as follows:

cmake_minimum_required(VERSION 2.8)
project(Cmain C CXX)

add_library(CPPclass STATIC IMPORTED)
set_property(TARGET CPPclass PROPERTY IMPORTED_LOCATION ./libCPPclass.a)
add_executable(main main.c)
target_link_libraries(main CPPclass)

但是,当我运行 cmake。 code> make 我得到与上面相同的错误

However, when I run cmake . and then make I get the same errors as above

$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/temp
$ make
Linking C executable main
./libCPPclass.a(CInt.o): In function `newCINT':
CInt.cpp:(.text+0xd): undefined reference to `operator new(unsigned long)'
CInt.cpp:(.text+0x28): undefined reference to `operator delete(void*)'

等。当然,如果我将 main.c 重命名为 main.cpp ,CMake将使用g ++编译可执行文件,target_link_libraries将执行没有错误,但它是一种失败的c包装的目的,并不工作在我真正的用例。

etc. Of course if I renamed main.c to main.cpp, CMake would compile the executable with g++ and target_link_libraries would execute without error, but it kind of defeats the purpose of the c wrapper and doesn't work in my real use case.

推荐答案

执行此操作:

set_target_properties(CPPclass PROPERTIES
  IMPORTED_LOCATION ./libCPPclass.a
  IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
)

这篇关于强制target_link_libraries使用c ++链接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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