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".
Related
I have two C programs:
Main.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("gcc x.c -o x");
system("x.exe");
return 0;
}
x.c
#include <stdio.h>
int main() {
printf("hello world!");
return 0;
}
From Main.c I need to compile and run x.c without using the command line. I've tried this but the code didn't work.
Thanks
This function call will compile your file and run it in one line :
system("gcc x.c -o x && x.exe");
I'm writing a C program and GCC doesn't recognise WIFCONTINUED. I've included the library that contains it (sys/wait.h) and checked that the library does exist on my machine.
My program:
#include <stdio.h>
#include <linux/limits.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "LineParser.h"
//Some code...
if(WIFCONTINUED(status)){
temp->status = RUNNING;
}
//More code...
The error I get when I compile is:
warning: implicit declaration of function ‘WIFCONTINUED’; did you mean ‘__W_CONTINUED’? [-Wimplicit-function-declaration]
else if(WIFCONTINUED(status)){
^~~~~~~~~~~~
__W_CONTINUED
undefined reference to `WIFCONTINUED'
collect2: error: ld returned 1 exit status
Has anyone experienced a similar problem with WIFCONTINUED?
EDIT:
Here is some example(not my original program) that failes to compile with the same errros:
#include <stdio.h>
#include <linux/limits.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char** argv){
int status;
if(WIFCONTINUED(status)){
printf("Works");
}
return 0;
}
My OS is Ubuntu 18.04 and my makefile is:
all: rep
rep: rep.o
gcc -g -Wall -m32 rep.o -o rep
rep.o: rep.c
gcc -g -Wall -m32 -ansi -c -o rep.o rep.c
.PHONY : clean
clean :
-rm -f *.o
The cause is the -ansi flag. It's equivalent to -std=c90 and WIFCONTINUED is not part of the C standard.
Most of the system programming headers you have included are from POSIX. So -ansi isn't going to help with anything at all. Just dropping the flag -ansi would be sufficient to fix this.
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 wrote this simple main.c file, to test microsoft's Raw Input API.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
RAWINPUTDEVICE Rid[1];
int main() {
printf("Hello world!\n");
return 0;
}
But i can't compile it, i receive the following error:
gcc main.c
main.c:4:1: error: unknown type name 'RAWINPUTDEVICE'
RAWINPUTDEVICE Rid[1];
^
i'm using Code::Blocks, but i recieve the same error using Prompt.
I find just this post with an resembling error.
I also tryed to rename the main file to main.cpp and compile it with the g++ command, but i reciebed another error:
C:\Users\msouza\Desktop\Raw Input>g++ main.cpp
main.cpp:4:1: error: 'RAWINPUTDEVICE' does not name a type
RAWINPUTDEVICE Rid[1];
^
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.