C语言 调用内联函数时未定义的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19068705/
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
undefined reference when calling inline function
提问by Marco Scannadinari
I am getting a really odd error from GCC 4.8.1 with inline functions.
我从带有内联函数的 GCC 4.8.1 得到一个非常奇怪的错误。
I have two near-identical inline functions defined in header files (debug.hand error.h) in src/include/, with the only difference being what they print - one prefixes DEBUG:to the message, and the other %s: error: %s(program name, error message). When defining the functions both inline, and compiling a debug build (so it sets the macro DEBUG=1), I get lots of undefined reference errors:
我在头文件(debug.h和error.h)中定义了两个几乎相同的内联函数src/include/,唯一的区别是它们打印的内容 - 一个DEBUG:是消息前缀,另一个是%s: error: %s(程序名称、错误消息)。在定义内联函数和编译调试版本(因此它设置宏DEBUG=1)时,我收到很多未定义的引用错误:
src/main_debug.o
gcc -osrc/main_debug.o src/main.c -c -Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g -DCC="\"gcc\"" -DCFLAGS="\"-Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g\"" -DDEBUG=1 -DBTCWATCH_VERSION="\"0.0.1\""
src/lib/btcapi_debug.o
gcc -osrc/lib/btcapi_debug.o src/lib/btcapi.c -c -Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g -DCC="\"gcc\"" -DCFLAGS="\"-Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g\"" -DDEBUG=1
src/lib/libbtcapi_debug.a
ar rc src/lib/libbtcapi_debug.a src/lib/btcapi_debug.o
ranlib src/lib/libbtcapi_debug.a
src/lib/cmdlineutils_debug.o
gcc -o src/lib/cmdlineutils_debug.o src/lib/cmdlineutils.c -c -Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g -DCC="\"gcc\"" -DCFLAGS="\"-Wall -Wextra -Wpedantic -std=gnu11 -march=native -Og -g\"" -DDEBUG=1
src/lib/libcmdlineutils_debug.a
ar rc src/lib/libcmdlineutils_debug.a src/lib/cmdlineutils_debug.o
ranlib src/lib/libcmdlineutils_debug.a
debug
gcc -obtcwatch-debug src/main_debug.o -Lsrc/lib/ -lbtcapi_debug -lcmdlineutils_debug -lcurl -ljansson
src/main_debug.o: In function `main':
/home/marcoms/btcwatch/src/main.c:148: undefined reference to `debug'
src/main_debug.o:/home/marcoms/btcwatch/src/main.c:185: more undefined references to `debug' follow
collect2: error: ld returned 1 exit status
make: *** [debug] Error 1
But changing debug()'s definition to static inlineremoves the errors. But I have never received any errors from error()'s definition, although its defenition is inline, and not static inline.
但是更改debug()的定义以static inline消除错误。但是我从来没有收到过任何错误error()的定义,尽管它的定义是inline,而不是static inline。
The definitions are all in headers (i.e. not prototyped)
定义都在标题中(即没有原型)
回答by Christoph
According to the manual, passing -std=gnu11enables C99 instead of GNU inline semantics.
根据手册,传递-std=gnu11启用 C99 而不是 GNU 内联语义。
This means inline, static inlineand extern inlineall behave differently. In particular, inlineexpects an external definition in a separate translation unit (which you can provide without duplicating the definition - see this answer).
这意味着inline,static inline并且extern inline所有行为都不同。特别是,inline需要一个单独的翻译单元中的外部定义(您可以在不复制定义的情况下提供 - 请参阅此答案)。

