C++ - 问题未定义对 PCM::getInstance() 的引用

C++ - Problem Undefined Reference to PCM::getInstance()

在我问一个新问题之前,我已经阅读了一些或更多关于这个的问题,但我一直很困惑。

我用 :

编译我的程序

g++ -std=c++11 -Wall -O3 -fopenmp main.cpp -o main -D WITH_COUNTER -I /usr/local/src/pcm -L /usr/local/src/pcm -L /usr/local/lib

然后,我发现了错误:

main.cpp:(.text.startup+0x27e): undefined reference to PCM::getInstance()

main.cpp:(.text.startup+0x289): undefined reference to PCM::resetPMU()

main.cpp:(.text.startup+0x310): undefined reference to PCM::program(PCM::ProgramMode, void const*)

所以,谁能告诉我如何解决这个问题?

您实际上 link 库本身。

-L 选项告诉 linker 添加一个目录到它的搜索路径,但是 linker 不会遍历它路径中的所有库来找到哪个可能是正确的(可能有数百甚至数千)。

相反,您需要使用 -l(小写 L)选项明确指定库 link。

对于某些示例库 foo,将存在一个名为 libfoo.alibfoo.so 的文件。要 link 使用它,请使用 -lfoo.

如果您的库的文档没有告诉您应该 link 使用哪个库,请查找合适的命名文件(如上所述)并使用正确的选项 link图书馆。