Problem with fonctions c file.

Hi!

I have a problem. I try to compile a simple "template" C program with the three classic files: main.c, fonctions.h and fonctions.c. I always work this way for many years on many IDE and I never had any compilation problem. But I tried to do the same at the command line in FreeBSD in a directory under root and I always get an "undefined reference" to the fonction1 in my fonctions.c file. Yet, all my files are ok because they compile and work very well in an IDE like Netbean.

Here's my template files:

main.c

Code:
#include <stdio.h>
#include <stdlib.h>
#include "fonctions.h"

int main(void)
{
        
    fonction1();
    
    return 0;
}

fonctions.h

Code:
#ifndef FONCTIONS_H_INCLUDED
#define	FONCTIONS_H_INCLUDED

int fonction1(void);

#endif

fonctions.c

Code:
#include <stdio.h>
#include <stdlib.h>
#include "fonctions.h"

int fonction1(void)
{
    printf("Hello world!");
    
    return 0;
}
I'm sure this code is ok but the cc and gcc command at the command line of FreeBSD don't find the fonction1 in fonction.c. All the files are in the same directory. I tried also a Makefile with a make depend, it doesn't work either.

Can anyone have an answer?

Thanks!
 
The code is fine, maybe wrong compiler call?
Code:
gcc -o helloworld main.c fonctions.c
should work.

p.s.: You should add code-tags when you post code on the forum and it's "functions" not "fonctions" :stud.
 
fonz said:
Linker actually, not compiler :)
Yes ;). Doing the compiling and linking in separate steps is probably a better example:
Code:
gcc -o fonctions.o -c fonctions.c
gcc -o main.o -c main.c
gcc -o helloworld main.o fonctions.o
 
Ok I see the problem. I'm new to command line compiler call, I mainly use IDE (Netbean, Eclipse, etc). I had to put my fonctions c file in the compiler call. I put only main.c. Sorry guys and thank you very much!
 
Note to add: a program consisting of two (or three, but not too many) source files is easily compiled (and linked) by hand. But for bigger projects it really pays to write a Makefile.

Fonz
 
Including function.h from your main.c should give fail if you trying to make two object files (main.o and function.o). This is because including function.h declares "int fonction1(void)" protoype which is not resolved without function.c. For this things there is a extern modifier in C language.

But easy way is just compile these three files as noted above
 
Alt said:
Including function.h from your main.c should give fail if you trying to make 2 object files (main.o and function.o).
No, the linking will fail but not the compilation of the source files.

This is because including function.h declares "int fonction1(void)" protoype which is not resolved without function.c. For this things there is a extern modifier in C language. But easy way is just compile these 3 files as noted above
The extern modifier is redundant for function declarations, it's implicitly there. For variables it tells the compiler that it is only the declaration of a variable and not the definition, so you can extent the visibility of global variables through different source files.
 
Alt said:
Including function.h from your main.c should give fail if you trying to make 2 object files (main.o and function.o).

I don't think you're right here. So how can you call the functions from the functions file otherwise? If you don't include functions.h in the main file, the main will not be able to get the prototypes of functions called in main. You'll get an "unresolve" error. There's no other way or you must type the prototypes directly in the main file and that's not the goal.
 
funky said:
The code is fine, maybe wrong compiler call?
Code:
gcc -o helloworld main.c fonctions.c
should work.

p.s.: You should add code-tags when you post code on the forum and it's "functions" not "fonctions" :stud.

It's "fonctions" for me because I'm french.
 
Ugluk911 said:
It's "fonctions" for me because I'm french.
Oh, sorry. I should not have judged you by my own standards, because I am very bad at orthography. Only with the aid of spell checking it's possible for me to write readable comments ;).
 
Ugluk911 said:
I don't think you're right here. So how can you call the functions from the functions file otherwise? If you don't include functions.h in the main file, the main will not be able to get the prototypes of functions called in main. You'll get an "unresolve" error. There's no other way or you must type the prototypes directly in the main file and that's not the goal.
Yes without prototype it will be "unresolve" error. But one *.o file should have declaration normal while other file should have somthing like this
Code:
extern int fonction1(void);
 
Alt said:
But one *.o file should have declaration normal while other file should have somthing like this
Code:
extern int fonction1(void);
First: there is no legible code like that in object files (*.o). You probably meant *.c file.

Second: That's what header files are for and that's exactly what the OP did. He omitted the extern keyword, but for functions that's ok (not the best style-wise, but correct).

Fonz
 
Back
Top