Skip to content

Instantly share code, notes, and snippets.

@semihkekul
Last active May 30, 2019 13:28
Show Gist options
  • Save semihkekul/87f9e263e9a804e2646764ec736062a3 to your computer and use it in GitHub Desktop.
Save semihkekul/87f9e263e9a804e2646764ec736062a3 to your computer and use it in GitHub Desktop.
void bar();
void foo(){
bar();
}
int main(){
foo();
return 0;
}
/******************************************************************/
$ g++ linking.cpp -save-temps
linking.o: In function `foo()':
linking.cpp:(.text+0x5): undefined reference to `bar()'
collect2: error: ld returned 1 exit status
$ nm linking.o
U _GLOBAL_OFFSET_TABLE_
U _Z3barv
0000000000000000 T _Z3foov
000000000000000c T main
void bar();
void foo(){
//bar();
}
int main(){
foo();
return 0;
}
$ g++ linking.cpp -save-temps
no error
$ nm linking.o
0000000000000000 T _Z3foov
0000000000000007 T main
void bar();
void foo(){
bar();
}
int main(){
//foo();
return 0;
}
$ g++ linking.cpp -save-temps -O1
linking.o: In function `foo()':
linking.cpp:(.text+0x5): undefined reference to `bar()'
collect2: error: ld returned 1 exit status
$ nm linking.o
U _GLOBAL_OFFSET_TABLE_
U _Z3barv
0000000000000000 T _Z3foov
000000000000000e T main
void bar();
static void foo(){
bar();
}
int main(){
//foo();
return 0;
}
$ g++ linking.cpp -save-temps -O1
no error
$ nm linking.o
0000000000000000 T main
void bar();
static void foo(){
bar();
}
int main(){
//foo();
return 0;
}
$ g++ linking.cpp -save-temps -O0
linking.o: In function `foo()':
linking.cpp:(.text+0x5): undefined reference to `bar()'
collect2: error: ld returned 1 exit status
$ nm linking.o
U _GLOBAL_OFFSET_TABLE_
U _Z3barv
0000000000000000 T _Z3foov
000000000000000c T main
/* linkingHelper.cpp content */
void bar(){}
/******************************************************************/
/* linking.cpp content */
void foo(){
bar();
}
int main(){
foo();
return 0;
}
$ g++ linking.cpp linkingHelper.cpp -save-temps -O1
no error
$ nm linking.o
U _GLOBAL_OFFSET_TABLE_
U _Z3barv
0000000000000000 T _Z3foov
000000000000000e T main
/* linkingHelper.cpp content */
static void bar(){}
/******************************************************************/
/* linking.cpp content */
void foo(){
bar();
}
int main(){
foo();
return 0;
}
$ g++ linking.cpp linkingHelper.cpp -save-temps -O1
linking.o: In function `foo()':
linking.cpp:(.text+0x5): undefined reference to `bar()'
linking.o: In function `main':
linking.cpp:(.text+0x13): undefined reference to `bar()'
collect2: error: ld returned 1 exit status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment