1

I have a header file included in the main but when I compile the main, I have an error saying that the linker failed.

I tried to find the object files but I cannot find them. I think the problem may come from my machine. I am kind of a beginner so I don't know how to solve this

When I try compiling my code I get this error:

Undefined symbols for architecture x86_64:
"_intClassic", referenced from:
      _main in main-53b7e4.o
  "_intQuadrature", referenced from:
      _main in main-53b7e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@zwol @JonathanLeffer I have 3 files in my project main.c, integral.h and integral.c. integral.c contains the code of the functions intClassic and intQuadrature that allow me to calculate different types of integral. In integral.h I declared the functions and structures I use. Finally in the main I included integral.h . Also $ gcc -o output file1.o file2.o can this command help me ?

mikek3332002
  • 3,546
  • 4
  • 37
  • 47
Sam_Bad
  • 15
  • 6
  • 2
    This error message has a lot of extra text that may be confusing you. The only important bits are `undefined symbol: _intClassic` and `undefined symbol: _intQuadrature`. You need to find the definitions of functions or data objects named `_intClassic` and `_intQuadrature`. (For reasons too complicated to get into here, their names might actually be `intClassic` and `intQuadrature`.) In order to help you any further than that I would need to know a lot more about the software you're trying to compile. – zwol Nov 13 '19 at 15:19
  • You need to include the header when compiling from C source to object file, and you need to include the library (via `-L /path/to/library` and `-llibname` options) when you link the code. You're getting the header OK; you're not including the library in the linking phase. Or you need to include more than one object file in the linking phase — it depends where `intClassic` and `intQuadrature` are defined. – Jonathan Leffler Nov 13 '19 at 15:23
  • @Sam_Bad You forgot to include the object file in the project that contains the definitions of the speified identifiers. – Vlad from Moscow Nov 13 '19 at 15:23
  • The "for architecture x86_64", while not meaningful to OP or to the programmer in general, is at least meaningful to us in that it tells us OP is using/building for Mac with fat binaries. – R.. GitHub STOP HELPING ICE Nov 13 '19 at 15:44
  • 1
    @zwol @JonathanLeffer I have 3 files in my project main.c, integral.h and integral.c. integral.c contains the code of the functions `intClassic` and `intQuadrature` that allow me to calculate different types of integral. In integral.h I declared the functions and structures I use. Finally in the main I included _integral.h_ . Also `$ gcc -o output file1.o file2.o` can this command help me ? – Sam_Bad Nov 13 '19 at 15:46
  • 2
    Well, show us the command you used when the error was reported. Then we might be able to help you better. – the busybee Nov 13 '19 at 16:15
  • 1
    `gcc *.c` might work. – jxh Nov 13 '19 at 16:27
  • @Yunnosch Please don't do that. Despite the corporate messaging and groupthink, the true value of SO _is_ personalized help. The archive of answered questions is only useful when a search turns up exactly what someone needs _and_ they realize that it's what they need. By the time someone has gotten around to posting a question, they are already convinced that their question is _not_ already answered, and that is true _for them_ even if someone more experienced would recognize one of the search hits as containing relevant information. – zwol Nov 13 '19 at 19:09
  • @Yunnosch Therefore: By all means mark questions as duplicates, because that helps with future searches, _but answer them anyway_ if there is enough information to do so. Don't nag people to think harder about potential duplicates, that will only discourage them. – zwol Nov 13 '19 at 19:10
  • Please share the code, also share how you are going to compile it. The problem might be related to the lack of the object file (.o) compiled(?) from the C file where you implemented the intClassic() and intQuadrature() functions that you did not use at link time. – dAm2K Nov 13 '19 at 20:32
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – S.S. Anne Nov 13 '19 at 20:55
  • 1
    @Yunnosch (The ideal thing would be for the SE _system_ to provide a way for us to answer duplicates _by explaining how the linked q&a answers the question_, because that's the hurdle that a beginner can't necessarily get over on their own. But that would require SE-the-organization to admit it has a problem. *hollow, bitter laughter*) – zwol Nov 13 '19 at 21:50
  • 1
    @zwol I see. Thanks for that perspective. I think I will refrain from fighting on your side, but I definitly see your point. Please feel cheered on. I will try harder to avoid the "do your homework first" impression, by more specifically proposing *how* exactly I believe an existing Q/A to answer and then ask to highlight the relevant difference. – Yunnosch Nov 13 '19 at 21:55

1 Answers1

2

In the same directory as your files, try running the command

gcc main.c integral.c -o integral

This should take the 2 files and compile them into a program called ./integral

mikek3332002
  • 3,546
  • 4
  • 37
  • 47