我在使用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之前,所以我不太瞭解如何進行鏈接。而這個錯誤對我來說並沒有什麼意義......
謝謝你幫我理解這個錯誤!
你沒有使用'g ++',你正在使用叮噹聲(蘋果正在對你說謊) –