Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to custom shared library

I create a .so file with the code below, but when I compile a file that invokes functions in the .so file with GCC, I get an "undefined reference to 'outlib1'" error.

What's wrong with my code or my command? Thanks.

OS Ubuntu 11.10
gcc 4.6.1

//file name outscreen.c

#include <stdio.h>

void outlib1(void)
{
    printf("out screen func1\n");
}
//file name main.c
int main(int argc, char* argv[])
{
    outlib1();
}
gcc outscreen.c -fPIC -shared -o outscreen.so   
gcc main.c -L. -loutscreen -o call   
./call
like image 772
jerrysea Avatar asked Feb 22 '23 04:02

jerrysea


2 Answers

Try:

$ gcc outscreen.c -fPIC -shared -o liboutscreen.so   
$ gcc main.c -L. -loutscreen -o call   

(note the change to the first line - the second line is unchanged)

like image 127
Paul R Avatar answered Mar 03 '23 13:03

Paul R


What is the output of this?

 nm outscreen.so | grep outlib1

Perhaps it is exporting with an underscore.

like image 28
buddhabrot Avatar answered Mar 03 '23 11:03

buddhabrot