2012-11-10 52 views
0

我從the website的壓縮文件中提取了源代碼,並將它們放在Code :: Blocks''include'文件夾中,但即使如此,它也無法編譯提供的'hello.cpp'示例。我是否需要構建TinyThread ++?

(僅供參考:)

#include <iostream> 
#include <tinythread.h> 

using namespace std; 
using namespace tthread; 


// This is the child thread function 
void HelloThread(void * aArg) 
{ 
    cout << "Hello world!" << endl; 
} 

// This is the main program (i.e. the main thread) 
int main() 
{ 
    // Start the child thread 
    thread t(HelloThread, 0); 

    // Wait for the thread to finish 
    t.join(); 
} 

而這些都是以下錯誤:

|41|undefined reference to `tthread::thread::thread(void (*)(void*), void*)'| 
|44|undefined reference to `tthread::thread::join()'| 
|44|undefined reference to `tthread::thread::~thread()'| 
|44|undefined reference to `tthread::thread::~thread()'| 

同樣的事情發生與wxDev-C++。我錯過了什麼;像,我需要建立圖書館或任何東西?如果是這樣,怎麼樣?

+1

你很可能需要鏈接到庫。這是一個非常好的錯誤提示:http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-doi-i-fix – chris

+0

@chris雖然這引發了我*很多最近的錯誤,我怎麼能鏈接庫?你或其他人可以向我解釋嗎? :O – Mutoh

+0

這裏不適用,但是如果你使用IDE,你通常可以進入鏈接器項目設置並添加庫。 MSVC允許你執行'#pragma comment(lib,「nameoflibrary.lib」)',並且通過命令行使用'-llibraryname'。 – chris

回答

3

從存檔內的readme.txt:

Using TinyThread++

To use TinyThread++ in your own project, just add tinythread.cpp and tinythread.h to your project. In your own code, do:

#include <tinythread.h> 
using namespace tthread; 

If you wish to use the fast_mutex class, inlude fast_mutex.h:

#include <fast_mutex.h> 

就包括頭部導致未解決的符號,因爲在.cpp沒有得到編譯。

+0

非常感謝您,將tinythread.cpp添加到項目中解決了它! – Mutoh

+0

嗯,這很有趣。我從來沒有見過一個庫讓你鏈接實現,而不是將它構建到庫中。 – chris

+0

只需要1個實現文件就可能會花費太多精力。此外它被廣告爲極簡主義。 –

0

TinyThread很小。

除了包含頭文件,您只需將TinyThread.cpp添加到您的項目中,或確保它作爲項目的一部分構建。

直到我將CPP文件添加到我的項目中,VC++中發生的特定錯誤。

線程類和方法沒有得到編譯,否則。

相關問題