2013-06-25 51 views
-1

我有客戶端服務器代碼client.cpp和server.cpp與main()。 服務器需要先執行並保持活動狀態直至不中斷。我們可以將兩個文件與main()放在makefile中嗎?

server.cpp包括我已經創建了兩個cpp文件:

#include "serverFunction.cpp" 
#include "serverFunction2.cpp" 

的這兩個還包括serverFunction.h

如何爲此編寫makefile?我在最後使用了pthread so -lpthread。 單獨我編譯以這種方式:

g++ -o a LinServer.cpp -lpthread 

我試着用這樣的:

all: LinServer LinClient 

LinServer: 
    g++ -o a LinServer.cpp -pthread 

LinClient: 
    g++ -o b LinClient.cpp -pthread 

但它給這個錯誤:

LinServer.o: In function `main': 
LinServer.cpp:(.text+0x6dd): undefined reference to `pthread_create' 
LinServer.cpp:(.text+0x6e9): undefined reference to `pthread_detach' 
LinServer.o: In function `__static_initialization_and_destruction_0(int, int)': 
LinServer.cpp:(.text+0xb3e): undefined reference to `std::ios_base::Init::Init()' 
LinServer.cpp:(.text+0xb55): undefined reference to `std::ios_base::Init::~Init()' 
LinServer.o:(.eh_frame+0x7b): undefined reference to `__gxx_personality_v0' 
collect2: error: ld returned 1 exit status 
make: *** [LinServer] Error 1 
+0

無關,但你不應該包括'.cpp' –

+2

**絕對不要''包括'.c/.cpp文件**。 –

+0

@ user174889嘗試將'g ++ -o LinServer.cpp -lpthread'更改爲'g ++ -o -lpthread LinServer.cpp' – tay10r

回答

1

您錯誤地指定的庫:

g++ -o a LinServer.cpp -pthread 

它應該是-lpthread,而不是-pthread-l選項意味着與庫進行編譯)。

你的問題與有兩個main()函數無關,但答案是 - 是的,你可以在同一個Makefile中用main()函數編譯兩個文件,但只有當這些文件屬於不同的輸出文件(不同的二進制文件)。

您的錯誤消息看起來像您的鏈接器設置或您的標準C++庫的配置有問題(鏈接器似乎無法看到它)。

+1

不,'-pthread'是正確的,因爲它修改編譯器的行爲,而不僅僅是鏈接到庫中。 – Potatoswatter

+0

在-lpthread的情況下,它會給出相同的錯誤! – user123

+0

@Patatoswatter哇,我不知道。非常感謝。 – Inspired

相關問題