我正在尝试创建我的第一个makefile.我使用以下命令测试了我的程序:
Command 1: gcc -Wall -ggdb3 -std=c99 -o file1 file1.c -lm -lpthread -l
Command 2: gcc -Wall -ggdb3 -std=c99 -o file2 file2.c -lm -lpthread
Run Code Online (Sandbox Code Playgroud)
一切都很好.然后我创建了一个makefile(请参见下文).我一直收到错误消息.有人可以看看我的代码,并给我一个问题是什么的提示?
file2.o: In function `seed_primes':
file2.c:(.text+0x461): undefined reference to `sqrt'
file2.c:(.text+0x466): undefined reference to `sqrt'
file2:(.text+0x533): undefined reference to `sqrt'
file2.o: In function `create_threads':
file2.c:(.text+0x668): undefined reference to `pthread_create'
file2.c:(.text+0x6b5): undefined reference to `pthread_join'
file2.o: In function `next_seed':
file2.c:(.text+0x860): undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [file2] Error 1
Run Code Online (Sandbox Code Playgroud)
这是我的makefile:
CC=gcc
DEBUG=-ggdb3
CFLAGS=#(DEBUG) -Wall -lm -lpthread -lrt -l
PROGS=file1 file2
all: $(PROGS)
file1: file1.o
$(CC) $(CFLAGS) -o file1 file1.o
file1.o: file1.c
$(CC) $(CFLAGS) -c file1.c
file2: file2.o
$(CC) $(CFLAGS) -o file2 file2.o
file2.o: file2.c
$(CC) $(CFLAGS) -c file2.c
clean:
rm -f $(PROGS) *.o *~
Run Code Online (Sandbox Code Playgroud)
由于#
注释字符(您可能打算使用$
替代),因此您已将CFLAGS设置为空字符串.
你不应该把库设置成CFLAGS
; 他们属于LDLIBS
.
您不需要file1:
规则,file2:
规则或目标文件规则.
CC = gcc
DEBUG = -ggdb3
CFLAGS = $(DEBUG) -Wall
LDLIBS = -lm -lpthread -lrt -l
PROGS = file1 file2
all: $(PROGS)
clean:
rm -f $(PROGS) *.o *~
Run Code Online (Sandbox Code Playgroud)
注意:LDLIBS
并且相关LDFLAGS
的变体不是100%均匀的make
. LDFLAGS
应该用于图书馆路径; LDLIBS
用于库名(-lxyz
等).
如果您需要为这两个程序使用不同的库,则需要创建单独的构建规则(如您最初的那样),或使用条件宏分配(GNU make
).