1

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 !

Cédric M.
  • 1,142
  • 3
  • 12
  • 23

2 Answers2

3

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();
}
user590028
  • 11,364
  • 3
  • 40
  • 57
  • I try to but it gives me "no such file or directory" error. I took a screenshot of my projects (I switched back on Ubuntu) : http://i.imgur.com/qxYvEIy.png. – Cédric M. Feb 06 '15 at 18:13
  • Your compiler can't find the header file. Check the path you've specified to `gcc`. Make sure you have a line that include the path to where `mylib.h` lives. For example `gcc -I/path/to/includes foo.c` – user590028 Feb 06 '15 at 19:16
  • It looks like it's ok but I take a screenshot of the build : http://i.imgur.com/ckK4iUF.png. Actually, the include path refers to the projet folder but not the one where the binaries are built. I'm gonna try to change de makefile. Your help is much appreciate, thanks ! – Cédric M. Feb 06 '15 at 21:04
  • I modified the path to be -I"/home/*****/workspace/mylib/Debug" instead of -I"/home/*****/workspace/mylib" but it still gives me the error. – Cédric M. Feb 06 '15 at 21:51
  • Try this -- copy the include file into the same folder as the foo.c file and compile. If that works than you can be convinced it's a path problem (which I'm certain it is) – user590028 Feb 07 '15 at 01:44
  • I should be able to include my library header file, right ? -- Owww ! I just did it ! Oh god thanks you for the help !!! It's kind of "messing up" (in fact, not at all) because of my folders structures. Since I'm not used to program in C, I tend to begin my projects in a "src" folder which contains all my sources. So, if I want to include my lib, I've to do "#include "../src/mylib.h". – Cédric M. Feb 07 '15 at 11:12
0

You should include

extern int foo();

in foo.c

then you can compile:

gcc -c mylib.c
gcc mylib.o foo.c -o foo

and execute:

./foo

As you are using eclipse, maybe it compiles corret after including the extern line and it's not needed to compile manually.

fcr
  • 14
  • 2