简体   繁体   English

如何将ac文件链接到两个可执行文件

[英]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.

相关问题 如何合并两个二进制可执行文件? - How do I merge two binary executables? 如何打开相对于 C 中可执行文件位置的文件? - How to open a file relative to the executables location in C? 用C编写自己的shell,如何运行Unix可执行文件? - Writing my own shell in C, how do I run Unix executables? 如何在 CMake 中构建两个共享主 function 的可执行文件? - How can I build two executables that share a main function in CMake? 如何在 C 编程中将两个头文件和 3 个 c 文件链接到一个可执行文件中? - How do I link two header files and 3 c files into a single executable in C programming? 如何使用 WINAPI 和 C++ 提取可执行文件的文件描述? - How to extract file description of executables using WINAPI and C++? 如何链接从 C 代码生成的目标文件、静态库和 NASM 生成的目标文件? - How do I link an object file generated from C code, a static library and a NASM generated object file? C Makefile 有两个可执行文件和一个公共目录 - C Makefile with two executables and a common directory 将两个二进制文件(可执行文件)打包到一个文件中 - Pack two binaries(executables) into one file 如何在C中使用头文件链接两个文件 - How to link two files using header file in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM