When I compile the below program it is giving me this error.
/tmp/ccwr6gsJ.o: In function 'main':
main.cL(.text+0xa): undefined reference to 'example'
collect2: error: Id returned 1 exit status
Main.c:
#include <stdio.h>
#include "includes.h"
int main()
{
int exampleInt = example();
return 0;
}
includes.h:
int example();
includes.c:
#include "includes.h"
int example()
{
int i = 3;
return i;
}
It seems to work in Visual Studio but not on GCC on Linux
This is very likely a build error, i.e. you're calling the compiler on the wrong set(s) of files, and/or not doing a linking step.
Try:
$ gcc -o myprog main.c example.c
Note that a mere #include in a C file does not in any way tell the compiler to compile more C files.
Related
I've a problem compiling the following programm:
// hauptteil.c (main part)
#include "nebenfkt.h"
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int x =10;
int ergebnis=0;
ergebnis =ver(x);
printf("Doubled number: %d", ergebnis);
return 0;
}
//nebenfkt.h
int ver(int x);
#include "nebenfkt.h"
#include <stdio.h>
#include <stdlib.h>
int ver(int x)
{
int rueck;
rueck= x*2;
return rueck;
}
VSC gives me the feedback "* undefined reference to `ver'
collect2.exe: error: ld returned 1 exit status*"
Solution
The problem occurred because I only used the command "gcc hauptteil.c -o function"
Instead of "gcc hauptteil.c nebenfkt.c -o function"
My mistake was discovered by Eugene Sh.
My mistake was that I only used the command "gcc hauptteil.c -o function" Instead of "gcc hauptteil.c nebenfkt.c -o function".
I'm trying to learn c and I implemented a bubblesort function and i decided It would be better idea if i made a library that will contain various sorting algorithms, so I compiled my code with this:
gcc -shared -fPIC -o bin/bsort.o sort/Bubblesort.c
my bubblesort.c is working (and not related to question at all and there is nothing other than bubblesort function there):
// Licensed under public domain with no warranty
void bubblesort(int* array) {
//implemention goes here
}
my sort.h file:
void bubblesort(int* array);
my nsort.c
#include "sort/sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main() {
int* sortthis = malloc(1000*sizeof(int));
for(int i = 0; i < 1000; i++) {
*(sortthis+i) = random(); //random int is defined somewhere else
}
bubblesort(sortthis);
for(int i = 0; i < 90; i++) {
printf("%d ",*(sortthis+i));
}
free(sortthis);
return 0;
}
my script that i use to compile:
gcc -shared -fPIC -o bin/bsort.o sort/Bubblesort.c
gcc nsort.c sort/sort.h -Lbin/bsort.o -lm -o demo.elf
what could be i'm doing wrong, i tried various things but it didn't work, i kept getting following error:
/usr/bin/ld: /tmp/ccxhd5zd.o: in function `main':
nsort.c:(.text+0x23): undefined reference to `bubblesort'
collect2: error: ld returned 1 exit status
gcc --version (just in case if there is a bug in this version):
gcc (Debian 10.2.1-6) 10.2.1 20210110
You don't put -L before the .o file. -L is for adding directories that -l searches for libraries.
To link with an object file, just add it as an ordinary file argument.
You also don't need to include header files in the compiler arguments. The compiler reads them when it sees #include.
gcc nsort.c bin/bsort.o -lm -o demo.elf
I wrote a program that in the main function calls a function from another .c file, but outputs an error
undefined reference to 'function_name'. collect2: error: ld returned 1 exit status.
I compile the program on the Linux command line: gcc -o main.exe main.c
./main.exe
funcs.h
#ifndef FUNCS_H_INCLUDED
#define FUNCS_H_INCLUDED
int foo();
#endif
second.c
#include <stdio.h>
#include "funcs.h"
int foo(){
printf("Hello, world!");
return 0;
}
main.c
#include <stdio.h>
#include "funcs.h"
int foo();
int main(){
foo();
return 0;
}
how to fix the error
You need to compile all c files, not only main.c
gcc -o main.exe main.c second.c
The undefined reference is raised when no symbol is detected for specific function.
In this case it can be fixed compiling also the .c file that contains the function so :
gcc -o main.exe main.c second.c
In this way you will have the symbol for the function you call that is in funcs.c file.
I'm testing the how to use extern in C ,so I create three files for main.c, test.c, headfile.h . I want to declare variable and function in headfile.h,define in the test.c ,then print out the variable and call function at the main.c
It works successfully by using Dev c++,however, when I put the exact same files into VScode it show errors that there are undefined reference to variables
the error messages
enter image description here
main.c
#include <stdio.h>
#include <stdlib.h>
#include"D:\My Documents\Desktop\CODE\c\VScode\externTest\headfile.h"
int gVar = 1;
int main(void)
{
extern float a;
printf("a = %f\n",a);
printf("gVar = %d\n",gVar);
printf("aa = %d\n",aa);
printf("bb = %f\n",bb);
function ();
system("pause");
return 0;
}
test.c
#include <stdio.h>
#include "D:\My Documents\Desktop\CODE\c\VScode\externTest\headfile.h"
float a = 100;
int aa = 200;
float bb = 300;
void function (void){
printf("yeh you got it!!\n");
extern int gVar;
gVar++;
printf("gVar in test.c function = %d",gVar);
}
headfile.h
extern int aa;
extern float bb;
void function(void);
It looks like your main.c file is not being linked with test.c.
I have been able to reproduce the exact same error message using the following compilation commands:
$ gcc main.c
/tmp/ccVqEXL5.o: In function `main':
main.c:(.text+0x8): undefined reference to `a'
main.c:(.text+0x38): undefined reference to `aa'
main.c:(.text+0x51): undefined reference to `bb'
main.c:(.text+0x69): undefined reference to `function'
collect2: error: ld returned 1 exit status
To fix this, simply add your test.c file to the compilation by either doing gcc main.c test.c.
The answer provided by #Ra'Jiska is correct but doing it is not straightforward if you want to compile your code using VScode. You need to indicate to tasks.json (file generated by VScode with the commands to compile the code) which extra files your program needs to compile.
More specifically, you need to add the line "${fileDirname}/test.c" in the "args" section of the "tasks" list.
If you are trying to include extra class files and this is your original run command:
cd "c:\myfolder\" ; if ($?) { g++ mainfile.cpp -o mainfile } ; if ($?) { .\mainfile}
add the extra file names (Shape.cpp & Circle.cpp for example) like this:
cd "c:\myfolder\" ; if ($?) { g++ mainfile.cpp Shape.cpp Circle.cpp -o mainfile } ; if ($?) { .\mainfile}
and run again
I know this question has a lot of answers, but I am always getting an error on gcc c1.c
c1.c:(.text+0x5): undefined reference to `f'
collect2: error: ld returned 1 exit status
no matter what I try.
This is c1.c
#include <stdio.h>
#include "c.h"
int main()
{
printf("F %d\n",f());
}
THis is c2.c
#include <stdio.h>
int f(void) {return 7;}
int main()
{
printf("S %d\n",f());
}
This is c.h
int f(void);
How could I get it working? Actually, I was getting this error in a big program that is modelled like this. I guess this is the way to do it.
(And yes, main should return 0).
You're going to have problems, since you have two different main functions defined, but the general way to do this is a link-time issue:
gcc -c c1.c
gcc -c c2.c
gcc c1.o c2.o
All you need to do is figure out which main you want, and remove the unwanted one.
You could do something like:
gcc -Dmain=blah -c c1.c
gcc -c c2.c
gcc c1.o c2.o