-3

I have two files. sim.c and devices.c.

Here's the sim.c

...
#include "devices.h"

int main(int argc, char **argv) {
  pthread_t *tid;

  tid = (pthread_t *) malloc(sizeof(pthread_t) * 3);

  // this is where I start the 3 threads located in devices.c
  if (pthread_create(&tid[0], NULL, device_one, NULL)) {
    exit(1);
  }
  if (pthread_create(&tid[1], NULL, device_two, NULL)) {
    exit(1);
  }
  if (pthread_create(&tid[2], NULL, device_three, NULL)) {
    exit(1);
  }

  // wait for 3 threads to finish
  int i;
  for (i = 0; i < 3; i++) {
    if (pthread_join(tid[i], NULL)) {
      exit(1);
    }
  }
}

Here's devices.c

...
#include "devices.h"

extern void *device_one(void *arg) {
  printf("device one is called\n");
  return NULL;
}

extern void *device_two(void *arg) {
  printf("device two is called\n");
  return NULL;
}

extern void *device_three(void *arg) {
  printf("device three is called\n");
  return NULL;
}

And here's devices.h

#ifndef DEVICES_H
#define DEVICES_H

extern void *device_one(void *arg);
extern void *device_two(void *arg);
extern void *device_three(void *arg);

However, when I compile, I get 3 errors under sim.c saying
undefined reference to 'device_one'
undefined reference to 'device_two'
undefined reference to 'device_three'

Dragneel
  • 171
  • 2
  • 16
  • 2
    Why are you declaring functions `extern` inside your `.c` implementation file? In the header file `extern` shouldn't be necessary either. Are you linking together both resulting `.o` files correctly to make the final executable? – tadman Mar 15 '18 at 20:18
  • "when I compile..." Well, *how* are you compiling? – Tavian Barnes Mar 15 '18 at 20:19
  • @TavianBarnes I compile by typing "make" – Dragneel Mar 15 '18 at 20:30
  • 1
    This might help you understand the use of `extern`. https://stackoverflow.com/a/1410632/1787434 – the_storyteller Mar 15 '18 at 20:32
  • 1
    @Dragneel Did you write the Makefile? What commands get run when you run `make`? – Tavian Barnes Mar 15 '18 at 20:33
  • @TavianBarnes No I never wrote it. It was given to me by my professor. Also, the skeleton of my files were written by my professor too. So he's the one who declared the externs in .c and .h. How do I find out what gets executed by "makefile"? – Dragneel Mar 15 '18 at 20:34
  • If you are having a problem with the makefile, then you should include it in the question. Unless you are suppressing the make output (using `@`), you normally see what compilation commands are executed. – P.P Mar 15 '18 at 20:39
  • Okay I just resolved the issue. I was using an old version of the provided makefile. It works now with the new makefile. – Dragneel Mar 15 '18 at 20:41

1 Answers1

1

The errors suggest you are not linking with the devices module when compiling sim.c (which contains main). You could compile as:

gcc sim.c devices.c -I.

Or you can create a makefile:

CC = gcc
CFLAGS = -I.
DEPS = devices.h
OBJ = sim.o devices.o
LDLIBS = -pthread

%.o: %.c $(DEPS)
        $(CC) -c -o $@ $< $(CFLAGS)

sim: $(OBJ)
        $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS)

.PHONY: clean

clean:
        rm -rf $(OBJ)
P.P
  • 117,907
  • 20
  • 175
  • 238