0

#Edited

I am tested on simple two files now that are add.h and add.c.

I made a Makefile in order to compile my program. Here is my makefile.

# Make file for running the project
CC=gcc
CFLAGS= -Wall -g
LDFLAGS = -include
OBJFILES = add.o
LIB = add.h
TARGET = add

all: ${TARGET}

%.o: %.c
    ${CC} ${CFLAGS} -c -o $@ $<

${TARGET}: ${OBJFILES}
    ${CC} ${CFLAGS} -o ${TARGET} ${OBJFILES}

clean:
    rm -f $(OBJFILES) $(TARGET) *~

when I run

make add

I get the following error:

gcc -Wall -g -c -o add.o add.c
gcc -Wall -g -o add add.o
Undefined symbols for architecture x86_64:
  "_b", referenced from:
      _main in add.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [add] Error 1

here are snippets of my code

add.c

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

int main(void) {
    int a = 10, b=20;
    add(a, b);
    return 0;
}

add.h

#ifndef __ADD_H_
#define __ADD_H_

extern int a,b;

int add(a,b)
{
    return a+b;
}

#endif // __ADD_H_
5
  • Use remake to debug your Makefile but do read the documentation of GNU make and try make -p; also read documentation of GCC Jun 28, 2020 at 12:30
  • 2
    1) Don't write this much code before testing it. 2) Do you give things names with leading underscores (e.g. _com_task)? Don't. 3) Read the page on minimal complete examples.
    – Beta
    Jun 28, 2020 at 12:46
  • @BasileStarynkevitch will have a look at the sources you provided. Thanks Jun 28, 2020 at 12:56
  • Read also Modern C and perhaps n1570, the C11 standard. See this C reference and learn to use GDB; there is no reason to have 3 header files in your case. Look for inspiration on some programs on github Jun 28, 2020 at 12:57
  • @Beta actually yes, I have given all my functions name starting or including _com_tasks. Do you think that can be the issue ? Jun 28, 2020 at 12:57

3 Answers 3

2

You are lacking a rule to actually build your object files. Right now you only have one to link them all together once they already exist. Try adding:

%.o: %.c
    ${CC} ${CFLAGS} -c -o $@ $<

This tells make how to build object files out of source files.

4
  • I have tried adding and compiling it but it's still showing the same error. Jun 28, 2020 at 12:09
  • You seem to be using _com_task in multiple functions, but you never defined it anywhere. Is there an implementation in any of the source files?
    – TsRoe
    Jun 28, 2020 at 12:17
  • com_task is a struct variable and I have used it only in stack.h and nowhere else. do I need to provide my whole source code here? Jun 28, 2020 at 12:49
  • I have compiled the program on the simplest version as you can see in the edited post. Jun 28, 2020 at 13:23
1

You are misusing the extern keyword, losing track of your variables and making your code too complicated.

Let's try something very simple:

int main(void) {
  int a = 10; b=20;

  return 0;
}

This fails. The compiler complains about the statement b=20;, since it has never heard of this b. The semicolon that made this a separate statement was either a typo or a conceptual error caused by declaring extern int b elsewhere. There is no need for extern here, at least not yet.

This:

int main(void) {
  int a=10, b=20;

  return 0;
}

works.

Now for an add function.

int add(int a, int b)
{
  return a+b;
}

int main(void) {
  int a = 10, b=20;

  add(a,b);

  return 0;
}

Note that the a and b in add are not the same variables as the a and b in main. This is crucial; do not proceed until you understand it.

Now add a declaration of the add function:

int add(int a, int b);  // <- declaration

int add(int a, int b)   // <- definition
{
  return a+b;
}

The declaration can be moved into a header file (add.h); the definition belongs in a source file (add.c).

Finally, I would advise you to add a line to the makefile:

add.o: add.h
1
  • waoooo they way you explained it cleared all my problems. I have read blogs but your explanation answered all my questions. Jun 28, 2020 at 16:53
1

Aren't you missing an #endif at the end of the last header file queue.h ? This can be for sure an issue when compiling. Moreover, aren't you missing the main_application.h header file you are then including within the stack.h one for example?

Have a look at this: https://medium.com/@m.muizzsuddin_25037/error-ld-symbol-not-found-for-architecture-x86-64-a5e5b648ffc seems helpful here and point our attention to the header files again!..

5
  • queue.h is having #endif Jun 28, 2020 at 12:00
  • Ok @owais, I was pretty sure of this, thus just add it editing your code snippet here above ;) Jun 28, 2020 at 12:02
  • I thought I might have done some mistake in declaring the headers Jun 28, 2020 at 12:05
  • What @tsroe is saying is for definitely right!.. About the "mistake in declaring the headers" I was wondering if the main_application.c file should instead have been named main_application.h or is it just a typo? If not you need anyway an header file there! Jun 28, 2020 at 12:07
  • 1
    Actually I have created a main_application.h just to define the variables that are used in other files such as queue and stack. I tried the solution provided by @tsroe but it's not working as well. Jun 28, 2020 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.