首页 > 解决方案 > 使用 gcc 编译时对 pow 的未定义引用

问题描述

pow在 C 中使用函数并包含math.h
以下是我得到的错误:

/usr/bin/ld: /tmp/ccUkOL31.o: in function `main': a1B.c:(.text+0xf3): undefined reference to 'pow' collect2: error: ld returned 1 exit status

虽然我在 StackOverflow 上读到需要链接。但这很奇怪。当我们使用gcc. g++似乎是自动执行的。

标签: cgccundefined-referencepow

解决方案


Is there any way to avoid that extra step when we are compiling with gcc?

The way I do it is having a script that invokes gcc (I have ~/bin in my SPATH)

$ cat ~/bin/mycc
#!/bin/sh
gcc -std=c11 -pedantic -Wall $* -lm
$ mycc example1.c example2.c

推荐阅读