簡體   English   中英

C++ 共享庫未定義對 `FooClass::SayHello()' 的引用

[英]C++ shared library undefined reference to `FooClass::SayHello()'

我正在制作一個 C++ 共享庫,當我編譯一個使用該庫的主 exe 時,編譯器給了我:

main.cpp:(.text+0x21): undefined reference to `FooClass::SayHello()'
collect2: ld returned 1 exit status

圖書館代碼:

fooclass.h

#ifndef __FOOCLASS_H__
#define __FOOCLASS_H__

class FooClass 
{
    public:
        char* SayHello();
};

#endif //__FOOCLASS_H__

fooclass.cpp

#include "fooclass.h"

char* FooClass::SayHello() 
{
    return "Hello Im a Linux Shared Library";
}

編譯:

g++ -shared -fPIC fooclass.cpp -o libfoo.so

主:main.cpp

#include "fooclass.h"
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    FooClass * fooClass = new FooClass();

    cout<< fooClass->SayHello() << endl;

    return 0;
}

編譯:

g++ -I. -L. -lfoo main.cpp -o main

該機器是 Ubuntu Linux 12

謝謝!

g++ -I. -L. -lfoo main.cpp -o main

是問題所在。 最新版本的 GCC 要求您按照相互依賴的順序放置目標文件和庫——根據經驗法則,您必須將庫標志作為鏈接器的最后一個開關; 即,寫

g++ -I. -L. main.cpp -o main -lfoo

反而。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM