sergio
sergio

Reputation: 75

C header files and dynamic linking error

I have created a dynamically linked library. The only problem I have is that my main program does not recognize my header file. The header file is in a separate folder from my main program. I have tried #include "myheader.h" as well as #include "/folder/dir/myheader.h"

Here is what my .h consist of

    extern int afunction(int,int);
    extern int afunction(int,int);

So far this code works

    gcc -fPIC -c filename1.c
    gcc -fPIC -c filename2.c

    gcc -shared -o libMylib.so filename1.o filename2.o

I then copy the lib to /usr/local/lib, and then

    gcc main.c -L. -lMylib -o exeName -ldl

and I get

    " myheader.h : no such file or directory.

Here is my directory structure:

directory1 ----------------folder1(main program.c)

directory1 ----------------folder2(myheader.h, along with functions for the pgm)

A push in the right direction would help, as I have written all my code and I am just at the last phase.

Upvotes: 0

Views: 1158

Answers (2)

Oki Sallata
Oki Sallata

Reputation: 374

You can put your lib header files in the same folder with your current program.

or like @Ed Heal said.. adding -I<path> for include header folder.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59987

You need gcc ... -I<some directory to myheader.h>. That will enable the compiler to find the header file.

Upvotes: 3

Related Questions