1

I am trying to compile a C program on my MacBook Pro, so far the files I have look something like this:

main.c

#include <stdio.h>
#include "blah.h"

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

blah.h

extern void method1()
extern void method2()
extern void method3()

blah.c

#include <stdio.h>
#include <stdlib.h>
#include "blah.h"

void method1()
{
    //do stuff
}

void method2()
{
    //do stuff
}

void method3()
{
    //do stuff
}

However, when I try to compile, I get an error like:

$ gcc main.c -o main
Undefined symbols for architecture x86_64:
  "_method1", referenced from:
      _main in ccVPIYad.o
  "_method2", referenced from:
      _main in ccVPIYad.o
  "_method3", referenced from:
      _main in ccVPIYad.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I think this is because Mac compiler adds an underscore in front of every function name, but I'm not sure how to fix this. I've tried changing blah.h to this:

extern void method1() asm ("create_list");
extern void method2() asm ("print");
extern void method3() asm ("insert_front");

but it is not fixing the problem.

b_pcakes
  • 2,452
  • 3
  • 28
  • 45

2 Answers2

2

The actual function definitions are in another source file which you are not compiling with.

Do:

 gcc blah.c main.c -o main
P.P
  • 117,907
  • 20
  • 175
  • 238
0
  1. What @l3x wrote
  2. Remove extern and add semicolons

Updated blah.h:

void method1();
void method2();
void method3();
Community
  • 1
  • 1
gollum
  • 2,472
  • 1
  • 18
  • 21