0

I have a weird problem in my Geany project. The project is extremely simple and contains 3 files all in the same directory: main.c, foo.h and foo.c.

Compiler error:

In file included from main.c:1:0:
foo.h:4:12: warning: ‘bar’ used but never defined
 static int bar(void);
            ^
/tmp/cc0zCvOX.o: In function `main':
main.c:(.text+0x12): undefined reference to `bar'
Compilation failed.
collect2: error: ld returned 1 exit status

What is going wrong?

main.c:

#include "foo.h"

int main(int argv, char* argc[])
{
    bar();
    return 0;
}

foo.h:

#ifndef _FOO_H_
#define _FOO_H_

static int bar(void);

#endif // _FOO_H_

foo.c:

#include "foo.h"

#include <stdio.h>

static int bar(void)
{
    printf("Hello World\n");
    return 1;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
sazr
  • 24,984
  • 66
  • 194
  • 362
  • A project is not including the correct call for gcc. Please update Build->Set Build commands and/or consider of using a makefile. You have first to compile foo.c and make it an object file and than compile main.c. Maybe the preset compile command for foo.c is working well for you, but you have to proof it. – frlan May 01 '15 at 08:35

1 Answers1

1

If a function is declared as static, the function is in file scope, means the scope of the function is limited to the translation unit only (in this case, the source file). Other functions which are present in the same compilation unit can call the functions, but no functions present outside the compilation unit can see the definition (presence) or call the function.

Related: From C11 standard document, chapter , linkage of identifiers

If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.(30)

and, footnote (30),

A function declaration can contain the storage-class specifier static only if it is at file scope;

Solution: Remove the static in the function definition and declaration.

FWIW, there is not much meaning of putting the forward declaration of a static function in a header file. Anyway, the static function can not be called from other source files.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I have removed `static` and it still says undefined reference to bar. Interestingly if I move the defintion of bar to foo.h (from foo.c) it compiles. This means that foo.c isn't included as part of the Geany project maybe? – sazr Apr 30 '15 at 05:20
  • @JakeM Remove `static` from header file also. And you don't define functions in header files. Maybe in this case, you're right, for some reason, `foo.c` is not getting compiled and linked with `main.c`. Check that please. – Sourav Ghosh Apr 30 '15 at 05:23
  • I have already removed `static` from both files. How do you add foo.c to a Geany project? I googled this and a stackoverflow answer said you dont need to as its linked automatically but maybe this isn't correct? – sazr Apr 30 '15 at 05:26
  • @JakeM I don't know what is Geany..sorry about that. A simple linux command line instruction will be `gcc main.c foo.c -o prog` where `prog` is the binary. See, if that helps. – Sourav Ghosh Apr 30 '15 at 05:27
  • @JakeM I guess you have a command line option there, so try running the above compilation statement. It should work. – Sourav Ghosh Apr 30 '15 at 05:32
  • Geany is saying that it is compiling the following `gcc -Wall -o "main" "main.c" (in directory: /home/me/Documents/Work/_tests/includes-test)` – sazr Apr 30 '15 at 05:33
  • @JakeM well, there you can see, it's not considering `foo.c`, right? You can add `foo.c` to same directory for build or adding the build path or something simmilar. otherwise, try the command line statement i suggested. it should work. :-) – Sourav Ghosh Apr 30 '15 at 05:38