[英]how do I link a c file to two executables
I have a file called wrapsock.c that has several wrapper functions for the socket.h header file. 我有一个名为wrapsock.c的文件,该文件具有用于socket.h头文件的多个包装函数。 I have two executables, FTP_client and FTP_server who both need to use the wrapsock.c file. 我有两个可执行文件FTP_client和FTP_server,它们都需要使用wrapsock.c文件。 I am having trouble with my makefile to link the wrapsock.c file to both of these programs 我的makefile遇到麻烦,无法将wrapsock.c文件链接到这两个程序
Here is my makefile below: 这是我的makefile如下:
all: ./bin/FTP_client ./bin/FTP_server
bin/FTP_client: main_client.o wrapsock.o
gcc -o bin/FTP_client main_client.o wrapsock.o -lpthread -g
mv main_client.o ./bin
mv wrapsock.o ./bin
bin/FTP_server: main_server.o wrapsock.o
gcc -o bin/FTP_server main_server.o wrapsock.o -lpthread -g
mv main_server.o ./bin
mv wrapsock.o ./bin
main_client.o: src/main_client.c
gcc -c src/main_client.c -g
wrapsock.o: src/wrapsock.c src/wrapsock.h
gcc -c src/wrapsock.c -g
main_server.o: src/main_server.c
gcc -c src/main_server.c -g
clean:
rm ./bin/*.o
rm ./bin/FTP_client
rm ./bin/FTP_server
This is the error I am receiving: 这是我收到的错误:
gcc -c src/main_client.c -g
gcc -c src/wrapsock.c -g
gcc -o bin/FTP_client main_client.o wrapsock.o -lpthread -g
mv main_client.o ./bin
mv wrapsock.o ./bin
gcc -o bin/FTP_server main_server.o wrapsock.o -lpthread -g
gcc: error: wrapsock.o: No such file or directory
make: *** [makefile:9: bin/FTP_server] Error 1
mv wrapsock.o ./bin
gcc -o bin/FTP_server main_server.o wrapsock.o -lpthread -g
gcc: error: wrapsock.o: No such file or directory
To move the object is not a good way, create the objects directly into the directory bin as you do for the executable : 要移动对象不是一个好方法,请像对可执行文件那样直接在目录bin中创建对象:
all: ./bin/FTP_client ./bin/FTP_server
bin/FTP_client: bin/main_client.o bin/wrapsock.o
gcc -o bin/FTP_client bin/main_client.o bin/wrapsock.o -lpthread -g
bin/FTP_server: bin/main_server.o bin/wrapsock.o
gcc -o bin/FTP_server bin/main_server.o bin/wrapsock.o -lpthread -g
bin/main_client.o: src/main_client.c
gcc -c src/main_client.c -g -o bin/main_client.o
bin/wrapsock.o: src/wrapsock.c src/wrapsock.h
gcc -c src/wrapsock.c -g -o bin/wrapsock.o
bin/main_server.o: src/main_server.c
gcc -c src/main_server.c -g -o bin/main_server.o
clean:
rm ./bin/*.o
rm ./bin/FTP_client
rm ./bin/FTP_server
Not sure why are you moving the binary objects to bin/
, but after you've built your client target wrapsock.o
is no longer in project root and resides in bin/
instead, so when server target gets its turn, gcc can no longer find it. 不知道为什么将二进制对象移动到bin/
,但是在构建客户端目标后, wrapsock.o
不再位于项目根目录中,而是位于bin/
,因此当服务器目标轮到时,gcc不再找到它。 I would just ditch the moves or if there is a reason for performing them, do so in a separate target that is done only after both binaries have been built (ie lists them as prerequisites). 我只想放弃这些动作,或者如果有执行它们的理由,那就在一个单独的目标中这样做,该目标只有在两个二进制文件都构建好之后才能完成(即将它们列为前提)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.