

* Translate user input to numbers, extra error checking Or: $ gcc -o working_program undefined_reference.c foo.cĪ more complex case is where libraries are involved, like in the code: #include įprintf(stderr, "Usage: %s \n", argv) $ gcc -o working_program undefined_reference.o foo.o Then the fix is to link both the object file from foo.c and undefined_reference.c, or to compile both the source files: $ gcc -c undefined_reference.c An alternative situation arises where the source for foo() is in a separate source file foo.c (and there's a header foo.h to declare foo() that is included in both foo.c and undefined_reference.c). To fix this error in our small program we would only have to add a definition for foo: /* Declaration of foo */ So we provided the compiler with the function header, but there was no such function defined anywhere, so the compilation stage passes but the linker exits with an Undefined reference error. We see here a declaration of foo ( int foo() ) but no definition of it (actual function).
#Pthread c language code#
So let's look at the code that generated this error: int foo(void) Undefined_reference.c:(.text+0x15): undefined reference to `foo'Ĭollect2: error: ld returned 1 exit status The error looks similar to this: $ gcc undefined_reference.c

One of the most common errors in compilation happens during the linking stage. Literals for numbers, characters and strings.Iteration Statements/Loops: for, while, do-while.Using character constants instead of string literals, and vice versa.Undefined reference errors when linking.Recursive function - missing out the base condition.Passing unadjacent arrays to functions expecting "real" multidimensional arrays.Newline character is not consumed in typical scanf() call.Mixing signed and unsigned integers in arithmetic operations.Mistakenly writing = instead of = when comparing.Ignoring return values of library functions.
#Pthread c language free#
