Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a source file in a C program

Tags:

c

How can I include foo() function of foo.c in this small program (sorry for my noob question):

In my foo.h file:

/* foo.h */
#include <stdio.h>
#include <stdlib.h>

int foo(double largeur);

In foo.c:

/* foo.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"

int foo(double largeur)
{
  printf("foo");
  return 0;
}

And in main.c:

/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"

int main(int argc, char *argv[])
{
  printf("Avant...");
  foo(2);
  printf("Apres...");
  return 0;
}

After compiling:

$ gcc -Wall -o main main.c

I get this error:

Undefined symbols: "_foo", referenced from: _main in ccerSyBF.o ld: symbol(s) not found collect2: ld returned 1 exit status

Thanks for any help.

like image 735
Denis Avatar asked Feb 26 '26 00:02

Denis


1 Answers

$ gcc -Wall -o main main.c foo.c

GCC doesn't know to look for foo.c if you don't tell it to :)

like image 145
bdonlan Avatar answered Mar 01 '26 10:03

bdonlan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!