C语言 简单 Makefile 的问题:对符号 'cos@@GLIBC_2.2.5' 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23809404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:06:38  来源:igfitidea点击:

Issue with simple Makefile: undefined reference to symbol 'cos@@GLIBC_2.2.5'

cmakefile

提问by jul

I'm trying to compìle some test C code using a Makefile. The main.cfile includes two headers:

我正在尝试使用 Makefile 编译一些测试 C 代码。该main.c文件包括两个标题:

#include "circle_buffer.h"
#include "window.h"

and when I execute the following Makefile

当我执行以下 Makefile 时

# compiler
CC=gcc

# compiler flags:
#  -g    adds debugging information to the executable file
#  -Wall turns on most, but not all, compiler warnings
CFLAGS=

# include path
INCLUDES =

# library path and names    
LIBS=-L/usr/lib/x86_64-linux-gnu -lsndfile

MAIN = test

$(MAIN): main.o circle_buffer.o window.o
    $(CC) main.o circle_buffer.o window.o -o $(MAIN) $(LIBS)

main.o: main.c circle_buffer.h window.h
    $(CC) -c main.c $(INCLUDES)

circle_buffer.o: circle_buffer.c circle_buffer.h
    $(CC) -c circle_buffer.c $(INCLUDES) 

window.o: window.h
    $(CC) -c window.c $(INCLUDES) 

.PHONY: clean

clean:
    rm -rf *o $(MAIN)

I get

我得到

xxx@xxx:~/source$ make
gcc -c main.c 
gcc -c circle_buffer.c  
gcc -c window.c  
gcc main.o circle_buffer.o window.o -o test -L/usr/lib/x86_64-linux-gnu -lsndfile
/usr/bin/ld: window.o: undefined reference to symbol 'cos@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [test] Error 1

If I remove #include "window.h"from main.c, and all the references to window.o/h/c in the Makefile, it works.

如果我#include "window.h"main.c, 以及 Makefile 中对 window.o/h/c 的所有引用中删除,它就可以工作。

What am I missing? Where am I breaking the rule

我错过了什么?我在哪里违反规则

target: dependencies
[tab] system command

?

?

回答by nos

Sounds like the math library, libmneeds to be linked in. Add -lmto the linking stage.

听起来像数学库,libm需要链接进去。添加-lm到链接阶段。

 LIBS=-L/usr/lib/x86_64-linux-gnu -lsndfile -lm

回答by Andrey Tzar

Also try to use c++ instead of gcc compiler. It includes this library by default.

也尝试使用 c++ 而不是 gcc 编译器。它默认包含这个库。