Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference error but symbol existing in the library

Tags:

c

gcc

linker

I get an undefined reference error for the example below. I have seen lots of questions that relate to this issue but believe I gave a stripped, reproducible, conceptual example as opposed specific issues in other questions,

dynlib.h:

void printMe_dyn();

dynlib.c:

#include <stdio.h>
#include "dynlib.h"

void printMe_dyn() {
  printf("I am execuded from a dynamic lib");
}

myapp.c:

#include <stdio.h>
#include "dynlib.h"

int main() 
{
    printMe_dyn();
    return 0;
}

Build steps:

gcc -Wall -fpic -c dynlib.c
gcc -shared -o libdynlib.so dynlib.o
gcc -Wall -L. -ldynlib myapp.c -o myapp

Error:

/tmp/ccwb6Fnv.o: In function `main':
myapp.c:(.text+0xa): undefined reference to `printMe_dyn'
collect2: error: ld returned 1 exit status

Proof that the symbol is in the library:

nm libdynlib.so | grep printMe_dyn
00000000000006e0 T printMe_dyn
  1. Am I using the correct compiler flags for building the dynamic library?
  2. Is the proof I've presented really an unambiguous proof?
  3. What other approach could be taken to diagnose the issue?
like image 402
TheMeaningfulEngineer Avatar asked Sep 03 '15 14:09

TheMeaningfulEngineer


People also ask

How do you fix a undefined reference error?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do you fix undefined symbol in C++?

You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.

What is undefined symbol error?

When generating an executable output file, the link-editor's default behavior is to terminate with an appropriate error message should any symbols remain undefined. A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition.

What is unresolved external symbol error in C++?

unresolved external symbol 'symbol' referenced in function 'function' The compiled code for function makes a reference or call to symbol, but the linker can't find the symbol definition in any of the libraries or object files.


2 Answers

The order of appearance of libraries matter.

To quote the online gcc manual

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

You should be changing your compilation statement to

gcc -o myapp -Wall -L. myapp.c -ldynlib 

to tell gcc to search for the symbols used in (compiled) myapp.c to be present in dynlib.

like image 172
Sourav Ghosh Avatar answered Oct 14 '22 22:10

Sourav Ghosh


Just as additional notice. The same behavior one may obtain when the library has been built by gcc and linked to the c++ project. Like follows:

gcc -Wall -fpic -c dynlib.c
gcc -shared -o libdynlib.so dynlib.o
g++ -o myapp -Wall -L. myapp.cpp -ldynlib

In such case the reason is name-mangling used by g++. To have it turned off one must wrap C-function prototypes by extern "C" within C-library. For example like follows:

dynlib.h:

#ifdef __cplusplus
extern "C"{
#endif
void printMe_dyn();
#ifdef __cplusplus
}
#endif
like image 21
Alexey Antonenko Avatar answered Oct 15 '22 00:10

Alexey Antonenko