c – 在Linux上链接共享库和未解析的符号
作者:互联网
我有以下三个项目:
>主机:导出全局变量的可执行文件(声明为extern)
>插件:由Host加载并引用全局变量的运行时库
>工具:链接插件并使用它的一些功能的可执行文件.它不以任何方式引用全局变量.
现在,如果我在Windows上构建它,一切都很好.该工具只链接到插件的导出库,不会尝试解析全局变量.
在linux上我遇到了一个问题.该工具尝试链接插件.so库(因为没有导出库),并将在Host中找到它无法解析的全局变量的引用.
如何解决这个问题呢?
编辑:
以下是使用CMake的问题的可编译示例.
的CMakeLists.txt
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/bin")
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/bin")
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
ADD_SUBDIRECTORY(Host)
ADD_SUBDIRECTORY(Plugin)
ADD_SUBDIRECTORY(Tool)
主机/ Host.h
#ifndef HOST_H
#define HOST_H
#ifdef _MSC_VER
#ifdef COMPILE_HOST
#define HOST_EXPORT __declspec(dllexport)
#else
#define HOST_EXPORT __declspec(dllimport)
#endif
#else
#define HOST_EXPORT
#endif
class HOST_EXPORT Host
{
public:
int getAnswer();
};
extern HOST_EXPORT Host g_host;
#endif
主机/ Host.cpp
#include "Host.h"
#include "../Plugin/Plugin.h"
#include <iostream>
Host g_host;
int Host::getAnswer()
{
return 42;
}
int main()
{
std::cout << g_host.getAnswer() << std::endl;
// load plugin and use it
}
主机/的CMakeLists.txt
PROJECT(Host)
ADD_EXECUTABLE(Host Host.cpp Host.h)
ADD_DEFINITIONS(-DCOMPILE_HOST)
SET_TARGET_PROPERTIES(Host PROPERTIES ENABLE_EXPORTS ON)
插件/ Plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H
class Plugin
{
public:
Plugin();
};
#endif
插件/ Plugin.cpp
#include "Plugin.h"
#include "../Host/Host.h"
#include <iostream>
Plugin::Plugin()
{
std::cout << g_host.getAnswer() << std::endl;
}
插件/ PluginFunc.h
#ifndef PLUGINFUNC_H
#define PLUGINFUNC_H
#ifdef _MSC_VER
#define PLUGIN_EXPORT __declspec(dllexport)
#else
#define PLUGIN_EXPORT
#endif
namespace plug
{
int PLUGIN_EXPORT getRandomNumber();
}
#endif
插件/ PluginFunc.cpp
#include "PluginFunc.h"
int plug::getRandomNumber()
{
return 4;
}
插件/的CMakeLists.txt
PROJECT(Plugin)
ADD_LIBRARY(Plugin SHARED Plugin.cpp Plugin.h PluginFunc.cpp PluginFunc.h)
TARGET_LINK_LIBRARIES(Plugin Host)
工具/ Tool.cpp
#include "../Plugin/PluginFunc.h"
#include <iostream>
int main()
{
std::cout << plug::getRandomNumber() << std::endl;
}
工具/的CMakeLists.txt
PROJECT(Tool)
ADD_EXECUTABLE(Tool Tool.cpp)
TARGET_LINK_LIBRARIES(Tool Plugin)
在Windows上,它构建并运行. Host.exe显示“42”,Tool.exe显示“4”.
在Linux上,我收到以下链接错误:
usr@debian64:~/vbox/testlink/build$make
Scanning dependencies of target Host
[ 25%] Building CXX object Host/CMakeFiles/Host.dir/Host.o
Linking CXX executable Host
[ 25%] Built target Host
Scanning dependencies of target Plugin
[ 50%] Building CXX object Plugin/CMakeFiles/Plugin.dir/Plugin.o
[ 75%] Building CXX object Plugin/CMakeFiles/Plugin.dir/PluginFunc.o
Linking CXX shared library libPlugin.so
[ 75%] Built target Plugin
Scanning dependencies of target Tool
[100%] Building CXX object Tool/CMakeFiles/Tool.dir/Tool.o
Linking CXX executable Tool
../Plugin/libPlugin.so: undefined reference to `Host::getAnswer()'
../Plugin/libPlugin.so: undefined reference to `g_host'
collect2: error: ld returned 1 exit status
make[2]: *** [Tool/Tool] Error 1
make[1]: *** [Tool/CMakeFiles/Tool.dir/all] Error 2
make: *** [all] Error 2
解决方法:
另外两种方法是:
>在Tool中定义全局变量.由于没有使用,因此值是什么并不重要.
>使用链接器命令定义具有组合值的符号.使用GCC和GNU ld它应该像gcc -Wl, – defsym = GlobalSymbol = 0
标签:c-2,linux,shared-libraries,cmake,dllexport 来源: https://codeday.me/bug/20190703/1365955.html