A simple error in the C built-in linker

simple problem:

given the following program:

#include <stdio.h> inline void addEmUp(int a, int b, int * result) { if (result) { *result = a+b; } } int main(int argc, const char * argv[]) { int i; addEmUp(1, 2, &i); return 0; } 

I get a linker error ...

 Undefined symbols for architecture x86_64: _addEmUp", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

It seems that he is not trying to compile it.

it does not need to be static , I would not think based on what I read:
Built-in linker error function (since it is in a different object and deals with 2 definitions, not zero)

This is a linked link, but it is C ++, and I don't think it is good practice in std C to put code in a header: the built-in linker error function

compiler information:

 cc --version Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) Target: x86_64-apple-darwin12.3.0 Thread model: posix 

compilation example:

 # cc main.c Undefined symbols for architecture x86_64: "_addEmUp", referenced from: _main in main-sq3kr4.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocatio 
+7
source share
2 answers

Paragraph 7 of section 6.7.4 states:

Any function with internal communication can be a built-in function. The following restrictions apply to an external link function: if the function is declared using the inline function specifier, then it must also be defined in the same translation unit. If all declarations of the scope for a function in a translation block include an inline function specifier without extern , then the definition in this translation unit is a built-in definition. The built-in definition does not provide an external definition for a function and does not prohibit an external definition in another translation unit. The built-in definition provides an alternative to an external definition that the translator can use to implement any function call in the same translation unit. It is not indicated whether the function call uses the built-in definition or the external definition .

Your file does not contain an external addEmUp definition, and the compiler decided to use an external definition in the main call.

Provide an external definition or declare it as static inline .

+11
source

Try adding the "-O" option to your compiler command. Enabling is enabled only when optimization is enabled.

+3
source

All Articles