链接错误使用 CMake 在C++中创建共享库,因为符号的多重定义
Linking error using CMake to create a shared library in C++ because multiple definition of symbol
我在编译与 POCO C++库链接的代码以创建 Linux 共享库(Mint 13,以防万一(时遇到问题。这是我减少的环境。
我有这 3 个文件:
IL_Notify.h
#ifndef __IL_NOTIFY_H__
#define __IL_NOTIFY_H__
#include <string>
#include "Poco/Logger.h"
#include "Poco/LogStream.h"
using Poco::Logger;
using Poco::LogStream;
namespace mynamespace
{
// Get logger. Inherits root channel
LogStream lstr(Logger::get("MyLogger"));
// From any other class, call logger this way:
//lstr << "This is a test" << std::endl;
}
#endif
IL_Class1.cpp
#include "IL_Class1.h"
#include "IL_Notify.h"
namespace mynamespace {
void IL_Class1::foo()
{
// Stuff...
lstr << "This is a test msg from IL_Class1::foo" << std::endl;
}
}
IL_Class2.cpp
#include "IL_Class2.h"
#include "IL_Notify.h"
namespace mynamespace {
void IL_Class2::bar()
{
// Stuff...
lstr << "This is a test msg from IL_Class2::bar" << std::endl;
}
}
IL_Class1
和IL_Class2
在IL_Class1.h
和IL_Class2.h
声明。这些标头中不包含IL_Notify.h
。
这是我的CMakeLists.txt文件
# Set search path to find PocoConfig.cmake
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules/)
# Look for needed packages
find_package(Poco REQUIRED)
# Now, we can use poco
include_directories(
${Poco_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
# Libraries which we are linking against
link_directories(
${Poco_LIBRARY_DIRS}
)
# Get all the cpp files to build my library
file(GLOB_RECURSE all_sources
${CMAKE_CURRENT_SOURCE_DIR}/src/IL_Class1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/IL_Class2.cpp
)
# Library creation
add_library(mylib SHARED ${all_sources})
我可以成功执行cmake
,以创建Makefile
.但是当我运行make
时,我收到此错误
CMakeFiles/mylib.dir/src/IL_Class2.cpp.o:(.bss+0x0): multiple definition of `mynamespace::lstr'
CMakeFiles/mylib.dir/src/IL_Class1.cpp.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [libmylib.so] Error 1
make[1]: *** [CMakeFiles/mylib.dir/all] Error 2
make: *** [all] Error 2
因此,即使我通过 #ifndef
阻止包含两次标头,make
也无法编译我的代码,因为该标头中声明的符号被检测到两次。为什么?
看到我的问题重复后,我得到了正确的解决方案。
我的IL_Notify.h
#ifndef __IL_NOTIFY_H__
#define __IL_NOTIFY_H__
#include <string>
#include "Poco/Logger.h"
#include "Poco/LogStream.h"
using Poco::Logger;
using Poco::LogStream;
namespace openil
{
// Get logger. Inherits root channel
extern LogStream lstr;
}
#endif
我的新IL_Notify.cpp
#include "IL_Notify.h"
namespace openil
{
// TODO: More flexibility here: more channels, different formatting...
// Maybe a IL_Utils function should do that
// Get logger. Inherits root channel
LogStream lstr(Logger::get("OpenILLogger"));
// From any other class, call logger this way:
//lstr << "This is a test" << std::endl;
}
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- dynamic_cast什么时候会因为隐藏符号而失败?
- 关于vtable符号可能未定义,因为类缺少其键函数
- c++链接错误2019无法解决的外部符号虚拟错误,因为接口…(?)
- 使用符号c++库:如何在objective C中打印矩阵输出描述?因为我加了两个矩阵.代码如下,谢谢
- 链接错误使用 CMake 在C++中创建共享库,因为符号的多重定义