c++ - 与共享库链接时 undefined reference

标签 c++ linker g++ static-linking

我正在尝试编写一个链接到共享库(* .so)的小程序。但是当我尝试编译程序时,
我收到一条错误消息,提示“函数”的“ undefined reference ”

程序文件的内容。

user@ubuntu:~/Perforce/user_ubuntu_3105/wp/eng/main/src/libfc/37D03B6/bin$ cat test.cpp 
#include <iostream>
#include <vector>
#include <memory>
#include "InfoModel.h"

int main() 
{
    libfc::InfoModel & model = libfc::InfoModel::instance();
    return 0;
}

编译这个我得到错误' undefined reference '
user@ubuntu:~/Perforce/user_ubuntu_3105/wp/eng/main/src/libfc/37D03B6/bin$ g++ -Wall -W -std=c++0x test.cpp -L. -lfc -o rst
test.cpp: In function ‘int main()’:
test.cpp:9:21: warning: unused variable ‘model’ [-Wunused-variable]
libfc::InfoModel & model = libfc::InfoModel::instance();
                 ^
/tmp/ccFtLDxc.o: In function `main':
test.cpp:(.text+0x9): undefined reference to `libfc::InfoModel::instance()'
collect2: error: ld returned 1 exit status

但是当我查看 .so 文件中的符号时,我看到了那里的符号。
user@ubuntu:~/Perforce/user_ubuntu_3105/wp/eng/main/src/libfc/37D03B6/bin$ nm --demangle libfc.so | grep InfoModel 
0000000000007e40 t _GLOBAL__sub_I_InfoModel.cpp
00000000002464a0 b guard variable for libfc::InfoModel::instance()::instance_
0000000000010260 t libfc::InfoModel::add_unknown(unsigned int, unsigned short, unsigned short)
0000000000010520 t libfc::InfoModel::registerIEType(libfc::IEType const*)
000000000000f550 t libfc::InfoModel::add(libfc::InfoElement const&)
0000000000012980 t libfc::InfoModel::add(std::string const&)
0000000000010a60 t libfc::InfoModel::instance()
00000000000106f0 t libfc::InfoModel::initTypes()
00000000000108f0 t libfc::InfoModel::InfoModel()
00000000000108f0 t libfc::InfoModel::InfoModel()
0000000000027ae0 t libfc::InfoModel::~InfoModel()
0000000000027ae0 t libfc::InfoModel::~InfoModel()
0000000000010b00 t libfc::InfoModel::parseIESpec(std::string const&) const
000000000000ee80 t libfc::InfoModel::lookupIEType(unsigned int) const
000000000000ed40 t libfc::InfoModel::lookupIEType(std::string const&) const
000000000000ef20 t libfc::InfoModel::dump(std::ostream&) const
000000000000f0c0 t libfc::InfoModel::lookupIE(unsigned int, unsigned short, unsigned short) const
000000000000f1b0 t libfc::InfoModel::lookupIE(libfc::InfoElement const&) const
0000000000012840 t libfc::InfoModel::lookupIE(std::string const&) const
000000000000f410 t libfc::InfoModel::lookupIE2(unsigned int, std::string const&, unsigned short, unsigned short) const
00000000002463a0 b libfc::InfoModel::instance()::instance_

我也这样做了。
user@ubuntu:~/Perforce/user_ubuntu_3105/wp/eng/main/src/libfc/37D03B6/bin$ readelf -s libfc.so | grep FILE 
34: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS EncodePlan.cpp
43: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS InfoElement.cpp
49: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS FileExportDestination.cpp
53: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS IETemplate.cpp
58: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS IEType.cpp
104: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS InfoModel.cpp
113: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS libfc.cpp
118: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS PlacementExporter2.cpp
124: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS PlacementTemplate.cpp
129: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS TemplateState.cpp
135: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.cpp
141: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS UdpSocketExportDestinatio
149: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
158: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS BasicOctetArray.cpp
159: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS Exception.cpp
160: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ExportError.cpp
161: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS FormatError.cpp
162: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS IESpecError.cpp
163: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS error_code.cpp
187: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
190: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS

更新:
构建这个共享库的 make 文件是
PLUGIN          = libfc
OUTPUT_DIR      = bin
OUTPUT          = $(OUTPUT_DIR)/$(PLUGIN).so
PLUG_SRC        = src
EXCEPTION_SRC   = src/exceptions
CPP             = g++
THIRD_PARTY_DIR = ../../../../../third-party
BOOST_DIR       = $(THIRD_PARTY_DIR)/boost/1.66.0
G3LOG_DIR       = $(THIRD_PARTY_DIR)/g3log/2017-07-18_g3log
FLAGS           = -c -std=c++0x -fvisibility-inlines-hidden -pthread \
              -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive 
MACROS          = -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_
INCLUDES        = -I$(PLUG_SRC) \
              -I$(EXCEPTION_SRC) \
              -I$(BOOST_DIR) \
              -I$(G3LOG_DIR)/src \
              -I$(G3LOG_DIR)/include
LFLAGS          = -shared -lpthread -L$(G3LOG_DIR)/build -lg3logger
CPPFLAGS     = $(FLAGS) $(MACROS) $(INCLUDES)  
LDFLAGS      = $(LFLAGS)
ifeq ($(BUILD),DEBUG)
CPPFLAGS += -ggdb3 -O0 
else
CPPFLAGS += -g -Wall -O2 -D NDEBUG
LDFLAGS += -flto 
endif

# compile static boost lib as
# ./bjam --toolset=gcc address-model=64 cxxflags=-fPIC cflags=-fPIC 
variant=release threading=multi link=static --with-system stage
BOOST_LIBS      = $(BOOST_DIR)/stage/lib/libboost_system.a
OBJ_DIR         = obj
PLUG_OBJS       = $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(subst 
$(PLUG_SRC)/,,$(wildcard $(PLUG_SRC)/*.cpp)))
FRAME_OBJS      = $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(subst 
$(EXCEPTION_SRC)/,,$(wildcard $(EXCEPTION_SRC)/*.cpp)))
VPATH           = $(PLUG_SRC) $(EXCEPTION_SRC)

all: $(PLUGIN)
$(PLUG_OBJS): $(OBJ_DIR)/%.o: %.cpp
   $(CPP) $(CPPFLAGS) -o $@ $<
$(FRAME_OBJS): $(OBJ_DIR)/%.o: %.cpp
   $(CPP) $(CPPFLAGS) -o $@ $<
$(PLUG_OBJS) $(FRAME_OBJS): | $(OBJ_DIR)
$(OBJ_DIR):
   mkdir $(OBJ_DIR)
$(OUTPUT): | $(OUTPUT_DIR)
$(OUTPUT_DIR):
   mkdir $(OUTPUT_DIR)
$(PLUGIN): $(PLUG_OBJS) $(FRAME_OBJS) $(OUTPUT)
   $(CPP) $(LDFLAGS) -o $(OUTPUT)  $(PLUG_OBJS) $(FRAME_OBJS) $(BOOST_LIBS)

.PHONY: clean
clean:
   rm -f $(PLUG_OBJS) $(FRAME_OBJS) $(OUTPUT)
   rm -rf $(OBJ_DIR)
   rm -rf $(OUTPUT_DIR)

make 的输出是
user@ubuntu:~/Perforce/sselvam_ubuntu_3105/wp/eng/main/src/libfc/37D03B6$ make all 
mkdir obj
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/BasicOctetArray.o src/BasicOctetArray.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/EncodePlan.o src/EncodePlan.cpp
src/EncodePlan.cpp: In constructor ‘libfc::EncodePlan2::EncodePlan2(const libfc::PlacementTemplate*)’:
src/EncodePlan.cpp:90:9: warning: unused variable ‘ie_present’ [-Wunused-variable]
bool ie_present
     ^
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/FileExportDestination.o src/FileExportDestination.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/IETemplate.o src/IETemplate.cpp
 g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/IEType.o src/IEType.cpp
 g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/InfoElement.o src/InfoElement.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/InfoModel.o src/InfoModel.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/libfc.o src/libfc.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/PlacementExporter2.o src/PlacementExporter2.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/PlacementTemplate.o src/PlacementTemplate.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/TemplateState.o src/TemplateState.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/UdpSocketExportDestination.o src/UdpSocketExportDestination.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/Exception.o src/exceptions/Exception.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/ExportError.o src/exceptions/ExportError.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/FormatError.o src/exceptions/FormatError.cpp
g++ -c -std=c++0x -fvisibility-inlines-hidden -pthread -fPIC -fvisibility=hidden -fno-omit-frame-pointer -fpermissive  -D_libfc_HAVE_NIXIO -U_libfc_HAVE_LOG4CPLUS_ -U_libfc_HAVE_WANDIO_ -Isrc -Isrc/exceptions -I../../../../../third-party/boost/1.66.0 -I../../../../../third-party/g3log/2017-07-18_g3log/src -I../../../../../third-party/g3log/2017-07-18_g3log/include -g -Wall -O2 -D NDEBUG -o obj/IESpecError.o src/exceptions/IESpecError.cpp
mkdir bin
g++ -shared -lpthread -L../../../../../third-party/g3log/2017-07-18_g3log/build -lg3logger -flto  -o bin/libfc.so  obj/BasicOctetArray.o obj/EncodePlan.o obj/FileExportDestination.o obj/IETemplate.o obj/IEType.o obj/InfoElement.o obj/InfoModel.o obj/libfc.o obj/PlacementExporter2.o obj/PlacementTemplate.o obj/TemplateState.o obj/UdpSocketExportDestination.o obj/Exception.o obj/ExportError.o obj/FormatError.o obj/IESpecError.o ../../../../../third-party/boost/1.66.0/stage/lib/libboost_system.a

任何帮助都会非常有帮助。

最佳答案

libfc::InfoModel & model = libfc::InfoModel::instance();
                 ^
/tmp/ccFtLDxc.o: In function `main':
test.cpp:(.text+0x9): undefined reference to `libfc::InfoModel::instance()'
collect2: error: ld returned 1 exit status


结合 -fvisibility=hidden (和 friend ):
$ nm -gCD ...

0000000000012980 t libfc::InfoModel::add(std::string const&)
0000000000010a60 t libfc::InfoModel::instance()
00000000000106f0 t libfc::InfoModel::initTypes()
...

我相信这一切意味着你隐藏了你的InfoModel类,它不再可见。我猜你手动添加了-fvisibility=hidden (和 friend )但没有编辑源文件并添加 DLL_PUBLIC (根据 GCC Visibility 维基)。

我认为对您来说最简单的解决方法是在不可见的情况下构建静态存档。即,不要使用 -fvisibility=hidden (和 friend )。然后,当您构建使用静态存档的共享对象/插件时,请使用 -fvisibility=hidden (和 friend )并包括 -Wl,--exclude-libs,ALL链接器标志。额外的链接器标志将确保您不会从静态文件中重新导出符号。

您的插件源文件将使用 DLL_PUBLIC (根据 GCC Visibility 维基)。 makefile 配方将使用如下内容:
$(PLUGIN): $(PLUG_OBJS) $(FRAME_OBJS) $(OUTPUT)
   $(CXX) -shared -o $(OUTPUT) $(CXXFLAGS) $(PLUG_OBJS) $(FRAME_OBJS) $(BOOST_LIBS) $(LDFLAGS) -Wl,--exclude-libs,ALL

然后,您可以检查从共享对象/插件导出的内容,例如:
nm -gCD libplugin.so | grep ' T '

不幸的是,没有办法避免 nm -gCD libplugin.so | grep ' T '疣。我只是在 Binutils 邮件列表中寻找一种更好的方式来显示导出。

最近针对 Issue 87190, Feedback on documentation for symbol visibility 的文档打开了一份 GCC 问题报告以供查看。 .另见 Linker exposing private symbols在 Binutils 邮件列表中。

一些额外的快速评论......

CPP = g++

CPP是预处理器。使用CXX .

FLAGS = ...


使用CXXFLAGS对于 C++ 项目。您的 C++ 配方可能如下所示:
SRCS = $(sort $(wildcard *.cpp))
OBJS = $(SRCS:.cpp=.o)

%.o : %.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<

关于c++ - 与共享库链接时 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213253/

相关文章:

c++ - 在这种情况下,可以避免在c++中手动管理内存吗?

c++ - 如何解决这些 libcurl 链接错误?

c++ - 如何从我的 Mac 上的终端窗口运行 g++

assembly - 在汇编代码中查找循环或数字

c++ - 逗号运算符的局限性

c++ - opencv waitkey,不精确,不起作用?

c++ - 删除动态数组后堆损坏

c++ - 如何将动态 dll 转换为静态库?

gcc - 使用链接器映射内存

c++ - g++ -Wall 没有关于 double-> int cast 的警告