Search code examples
ceclipsemakefileshared-librariesmulti-project

Using my own library : implicit declaration of function


Firstly, I'd like to thanks you in advance for the time you'll take to help me out. If I may suggest you, you can try reproduce my problem. Don't try to read the makefiles if you don't feel it'll help you to understand my problem.

Also, I'd like to point out the fact I did a lot of researches and I don't have find any solution.

My Environment


  • Eclipse (with CDT)
  • Windows (cygwin) (but I also tried on Ubuntu)

I want to use my own (shared) library in a project.

My Setup


My Shared Library

mylib.h

#ifndef MYLIB_H_
#define MYLIB_H_

extern int foo();

#endif /* MYLIB_H_ */

mylib.c

#include "mylib.h"

extern int foo() {
    return 1;
}

My Project

I added my library as a reference :

Project Properties - C/C Generals - Paths and Symbols - References (tab) - Check off mylib (Active)

foo.c

#include <stdlib.h>

int main(int argc, char **argv) {
    return foo();
}

Problem


I'm getting implicit declaration of function 'foo' [-Wimplicit-function-declaration] warning when I build my project. This warning only occurs when I build my project while my library project has nothing to build (because it hasn't been modified since the last build).

Console output

Info: Internal Builder is used for build
gcc -std=c99 "-ID:\\Users\\cmourgeo\\maximo workspace\\mylib" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\uselib.o" "..\\src\\uselib.c" 
..\src\uselib.c: In function 'main':
..\src\uselib.c:12:2: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration]
  return foo();
  ^
gcc "-LD:\\Users\\cmourgeo\\maximo workspace\\mylib\\Debug" -o uselib.exe "src\\uselib.o" -lmylib 

Should I provide eclipse my own makefiles ? (under C/C++ / Builder Settings)

Solution


I had to include my header in foo.c

#include "../src/mylib.h"

The path is kind of weird because of my projects structures :

  • myproject

    • src
      • foo.c
  • mylib

    • src
      • mylib.c
      • mylib.h

Thanks to user590028 for helping me getting through that !


Solution

  • In foo.c you forgot to include the mylib.h header

    /* foo.c */
    
    #include <stdlib.h>
    #include "mylib.h"  /* <-- include this line */
    
    int main(int argc, char **argv) {
        return foo();
    }