[英]CMake “OBJECT” library: clang not linking properly
I have a shared library (which currently compiles, loads and runs) mylib.so
. 我有一个共享库(目前正在编译,加载和运行)
mylib.so
。 From within this library, I want to use a new function (register it in another external library). 从这个库中,我想使用一个新函数(在另一个外部库中注册)。 The signature is
bool my_function(const QVariant *, PyObject **)
. 签名是
bool my_function(const QVariant *, PyObject **)
。
This new function is defined in a separate .cpp file which is compiled to an object and then linked to mylib.so
. 这个新函数在一个单独的.cpp文件中定义,该文件被编译为一个对象,然后链接到
mylib.so
。
So I create a new OBJECT
with my custom function 所以我用我的自定义函数创建了一个新的
OBJECT
ADD_LIBRARY(helper_lib OBJECT helper_lib.cpp)
And include this when building my library 在构建我的库时包括这个
ADD_LIBRARY(mylib SHARED source.cpp $<TARGET_OBJECTS:helper_lib>)
It fails with an "undefined reference to `my_function'" 它失败了“对'my_function'的未定义引用”
I can see that 我知道
helper_lib.o
file is generated helper_lib.o
文件 nm helper_lib.o
shows nm helper_lib.o
显示
0000000000000000 T _Z11my_functionPK8QVariantPP7_object
nm mylib.o
shows nm mylib.o
显示
U my_function
The helper_lib.o
is passed to clang++ : helper_lib.o
传递给clang ++:
clang++ -fPIC [...] -o my_lib.so mylib.o helper_lib.o [...]
I struggle to see where the mistake happens. 我很难看出错误发生在哪里。 I can imagine that there is something wrong in
mylib.o
which shows an unmangled symbol name which cannot be matched to the helper_lib.o
symbol name, but I may as well be totally on the wrong track with this. 我可以想象
mylib.o
中有一些错误,它显示了一个无符号的符号名称,它不能与helper_lib.o
符号名称匹配,但我也可能完全在错误的轨道上。
helper_lib.h helper_lib.h
void my_function();
helper_lib.cpp helper_lib.cpp
#include "helper_lib.h"
void my_function()
{
return;
}
source.cpp is more complicated, as it contains mainly code automatically generated by sip . source.cpp更复杂,因为它主要包含由sip自动生成的代码。
It works for me with a simple source.cpp. 它适用于简单的source.cpp。 So it must be that something gets messed up during inclusion, you can try moving
#include "helper_lib.h
to the top of your source.cpp. 因此,必须在包含期间弄乱某些东西,您可以尝试将
#include "helper_lib.h
移动到source.cpp的顶部。
To verify that this has nothing to do with your toolchain, you can try from a clean build directory the following project: 要验证这与您的工具链无关,您可以从干净的构建目录中尝试以下项目:
CMakeLists.txt: 的CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(dummy)
ADD_LIBRARY(helper_lib OBJECT helper_lib.cpp)
ADD_LIBRARY(mylib SHARED source.cpp $<TARGET_OBJECTS:helper_lib>)
source.cpp: source.cpp:
#include "helper_lib.h"
void dummy() {
my_function();
}
helper_lib.h: helper_lib.h:
#pragma once
void my_function();
helper_lib.cpp: helper_lib.cpp:
#include "helper_lib.h"
void my_function() {
}
Some documentation . 一些文件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.