MacOSX共享库:架构x86_64的未定义符号
问题描述:
我在使用MacOSX上的共享库编译代码时遇到了一些麻烦。 我首先在Debian上编写它之前,试图在MacOSX上编译它。MacOSX共享库:架构x86_64的未定义符号
下面是代码:
test.hxx:
#ifndef TEST_HXX
#define TEST_HXX
namespace test
{
class CFoo
{
/* data */
public:
CFoo(){}
virtual ~CFoo(){}
void bar();
}; /* CFoo */
} /* namespace test */
#endif /* TEST_HXX */
test.cxx:
#include <iostream>
#include "test.hxx"
void test::CFoo::bar()
{
std::cout << "Hello world!" << std::endl;
} /* bar() */
other.hxx:
#ifndef OTHER_HXX
#define OTHER_HXX
namespace other
{
class CBar
{
public:
CBar(){}
virtual ~CBar(){}
void foo();
}; /* CBar */
} /* namespace other */
#endif /* OTHER_HXX */
other.cxx:
#include <iostream>
#include "test.hxx"
#include "other.hxx"
void other::CBar::foo()
{
test::CFoo c;
c.bar();
} /* bar() */
main.cxx:
#include "other.hxx"
int main (int argc, const char *argv[])
{
other::CBar c;
c.foo();
return 0;
} /* main() */
和一个简单的生成文件:
LIBTEST = libtest.so
LIBOTHER = libother.so
all: $(LIBTEST) $(LIBOTHER)
g++ -ltest -lother -I. -L. main.cxx
libtest.so: test.o
g++ -shared test.o -o $(LIBTEST)
libother.so: other.o
g++ -shared other.o -o $(LIBOTHER)
test.o: test.cxx test.hxx
g++ -fPIC -c test.cxx
other.o: other.cxx other.hxx
g++ -fPIC -c other.cxx
clean:
$(RM) $(LIBOTHER) $(LIBTEST) test.o other.o a.out
所以我基本上创建对象test.o
和other.o
并从中创建两个共享库(每个对象一个)。
other.cxx
使用test.cxx
中包含的类别打印0。
所以此生成的文件和代码工作在我的Debian的罚款,但我试图编译它的MacOSX当编译错误:创建libother.so
当
g++ -fPIC -c test.cxx
g++ -shared test.o -o libtest.so
g++ -fPIC -c other.cxx
g++ -shared other.o -o libother.so
Undefined symbols for architecture x86_64:
"test::CFoo::bar()", referenced from:
other::CBar::foo() in other.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libother.so] Error 1
它编译和test
创建我的共享库,但失败。
当仅使用一个共享库(所以打印在main
直接从test
Hello world
),它工作得很好,但使用多个共享库时出现问题...
我不是苹果的用户,从来没有工作在MacOSX之前,所以我不太了解如何进行链接。而这个错误对我来说并没有什么意义......
谢谢你帮我理解这个错误!
答
这是因为libother
使用libtest
但您不链接它。尝试
g++ -shared other.o -o libother.so -L. -ltest
的-L
告诉编译器到哪里寻找库,它-l
链接。
+0
太棒了,它正在工作!谢谢你的帮助! – user
你没有使用'g ++',你正在使用叮当声(苹果正在对你说谎) –