-1

我有客户端服务器代码client.cpp和server.cpp都带有main()。服务器需要首先执行并保持活动状态,直到不被中断。

server.cpp 包含我创建的两个 cpp 文件:

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

这两者还包括serverFunction.h.

如何为此编写makefile?我最后使用了 pthread 所以 -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
4

1 回答 1

1

您错误地指定了库:

g++ -o a LinServer.cpp -pthread

它应该是-lpthread,不是-pthread(该-l选项意味着使用库编译)。

您的问题与拥有两个 main() 函数无关,但答案是 - 是的,您可以在同一个 Makefile 中使用 main() 函数编译两个文件,但前提是文件属于不同的输出文件(不同的二进制文件)。

您的错误消息看起来像是您的链接器设置或标准 C++ 库的配置有问题(链接器似乎看不到它)。

于 2013-06-25T05:04:58.720 回答