当我尝试使用gcc在WSL环境中制作udp_echo_server.c
的可执行文件,其中包括basic_socket.h
(下面附有代码)时:
gcc -I/mnt/c/Users/USER/hjkim/tcp_ip /mnt/c/Users/USER/hjkim/tcp_ip/udp_echo_server.c -o udp_echo_server -std=gnu11
我以为它可以用,但是出现了这样的错误。
/tmp/ccJCFe4E.o: In function `main':
udp_echo_server.c:(.text+0x36): undefined reference to `print_arg_error'
udp_echo_server.c:(.text+0x63): undefined reference to `print_socket_error'
collect2: error: ld returned 1 exit status
我给了-v
gcc选项,它说:
#include "..." search starts here:
#include <...> search starts here:
/mnt/c/Users/USER/hjkim/tcp_ip
/usr/lib/gcc/x86_64-linux-gnu/7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
所以,问题是:我想我已经给出了正确的-I
选项来告知basic_socket.h
的位置,但是gcc似乎没有得到。我应该给其他选择吗?还是我在源代码中犯了任何错误?
我的源代码如下:
// basic_socket.h
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUF_SIZE 1024
void print_arg_error() {
printf("argc error\n");
exit(1);
}
void print_socket_error() {
printf("socket create error\n");
exit(1);
}
// udp_echo_server.c
#include "basic_socket.h"
int main(int argc, char* argv[]) {
if (argc != 3) {
print_arg_error();
}
int sock;
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
print_socket_error();
}
.... [omitting the rest for simplification] ...
return 0;
}