Gcc compilation error when compiling a *.cc file - c

I have written a simple C program using gcc compiler in Ubuntu enviroment. The code is simple. Howver, when i try to compile, it is giving an error which I am not able to fathom. Here is the code and the error
# include <stdio.h>
int main() {
enum mar_status {
single,married,divorced
};
enum mar_status person1,person2;
person1 = single;
printf("%d\n",person1); //line B
}
I am getting the following error when I compile
gcc enum2.cc
/tmp/cc6stgaW.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status
If I remove the printf statement at line B, everything goes fine. Any ideas as to why the compilation is failing ?

You are using gcc to compile C++ code? (.cc extension)
Either rename the file to enum2.c or compile with g++.

Undefined references to internal run-time library functions, such as __gxx_personality_v0, are also a symptom of linking C++ object files with gcc instead of g++.
Changing file extension from .cc or .cpp to .c will resolve the issue.

You are confusing the compiler - when you say:
gcc enum2.cc
it thinks you are compiling C++ code, but you are doing it with gcc, which doesn't link the correct C++ libraries. Use:
gcc enum2.c

Its running fine. Check : http://ideone.com/bhjlf
I guess your command to compile is wrong.

Related

Using vscode, why can't I compile when both "iosteam" and "stdio.h" are included?

When I declare and at the same time, vscode can't complie the file and there is the error log:
> Executing task: C:\mingw64\bin\gcc.exe -g d:\CODES\C++\try\main.cpp -o d:\CODES\C++\try\main.exe <
C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__tcf_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__static_initialization_and_destruction_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
My code is very simple:
#include <iostream>
#include <stdio.h>
int main()
{
printf("print something");
return 0;
}
Dev-C++ can compile that code properly.
If I delete #include <iostream>, it can be properly compiled.
<iostream> is a standard C++ header, so you need a C++ compiler and linker to use it. gcc is smart enough to compile .cpp files as C++, however it won't link the standard library into your program, hence the undefined references.
You need to use g++, which will link the standard C++ library by default, as Dev-C++ does.

How to link the cs50 C library in gcc on windows

I'm new to ะก programming and have been trying to compile my code using MinGW/GCC, but I try to include cs50 (cs50.c, cs50.h) library, and the compiler can't find it. Help me compile who knows what's going on.
I tried to give such command: gcc -LC:\Users\apple\Desktop -lcs50 mario.c
But the result is this:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lcs50
collect2.exe: error: ld returned 1 exit status
Or:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\apple\AppData\Local\Temp\cc8KpeUr.o:mario.c:(.text+0x33): undefined reference to `GetInt'
collect2.exe: error: ld returned 1 exit status
This is my code:
#include <stdio.h>
#include <cs50.h>
int main()
{
int num = GetInt();
printf("%d\n",num);
}
gcc -LC:\Users\apple\Desktop -lcs50 mario.c
There are two problems here.
Always pass libraries after .c files or they won't actually do anything (unless main is in the library).
You appear to have a library called cs50.a; -lcs50 wants to find a file called libcs50.a or libcs50.so.
The easiest way around this problem is to not bother with -L or -l and just pass your library directly to gcc like this:
gcc mario.c cs50.a
Since cs50.c is a single file, you do not need a library at all. You can compile it as needed to save a few steps, it will consume a couple milliseconds more but most of the time you would not notice.
Just use
gcc mario.c cs50.c
and it will work (provided that both files are in the current folder).
I had the same problem. What i did was that i put the cs50.h and cs50.c files in the same folder or directory as stdio.h ; which you will find in the program files of the compiler you're using. It worked for me. Hope this helps.

Error in C program using Fedora

I get the following error when dealing with C using Fedora:
[king#localhost ~]$ gcc -o1 tempdaa.c
tempdaa.c:3:17: fatal error: queue: No such file or directory
#include <queue>
^
compilation terminated.
Any ideas on where the problem is?
gcc is generally what you use to compile C code. If you want to compile C++ code, you'd tend to use g++.
Now it's true that gcc can compile C++ if it's clear you have a C++ program but I think, from memory, that's indicated by the extension rather than the content.
Since your extension is .c rather than something like .cpp or .cc or .cxx, it will definitely think it's C code and behave accordingly.
Hence the C++ header queue will not be available to you.
My suggestion is that you name your C++ source files "correctly", or force the language type explicitly:
gcc -x c++ -o1 tempdaa.c

Eclipse fails to compile C project when using a flags

I'm trying to compile a C project with Eclipse, under Ubuntu 12.10 using GCC.
When I use these flags:
http://i.stack.imgur.com/FTxkG.jpg
http://i.stack.imgur.com/MVA2k.jpg
eclipse fails to compile.
The compilation error is the following:
error: ISO C forbids an empty translation unit [-pedantic]
make: *** [.metadata/.plugins/org.eclipse.cdt.make.core/specs.o] Error 1
Why do these problems appear,
and how should I compile with eclipse with the checked flags?
When trying to compile manually from the Terminal with these flags, it compiles successfully. thanks!
Your C project has an empty source file.
You can either:
1) Remove that file. If it is empty why do you need it?
2) Be more relax with your compilation flags, with -pedantic instead of -pedantic-errors for example
3) Add some dumb code to make gcc believe the file is not empty, for example add #include <stdio.h>.

Compiler is able to find function without matching .h file is updated?

I'm writing a C University project and stumbled upon a compiler behavior which I don't understand.
In this file http://code.google.com/p/openu-bsc-maximveksler/source/browse/trunk/20465/semester/tasks/maman14/alpha/maman14/assembler/phaseOne.c?r=112 I've added a call to function named freeAsmInstruction(). This function is defined in file named lineParser.c, yet I haven't updated the matching lineParser.h header file to include this function declaration.
Why does this code compile? I would expect that gcc would fail to compile phaseOne.c until the correct lineParser.h is updated with the declaration of freeAsmInstruction().
I would appreciate an explanation.
Thank you,
Maxim
The GCC compiler is assuming a particular default function signature. To get a warning about this, compile with the -Wall flag:
gcc -Wall -c phaseOne.c
this will give you a warning of the form:
phaseOne.c:2: warning: implicit declaration of function 'your func here'
Unless you have good reason, you should always compile with the -Wall flag, and probably other warning flags too.
Undefined functions are not automatically an error; the compiler takes them to have a default prototype, then when your code is finally linked if there is something there of the right name it will be used. However, if the default prototype isn't what your function actually has, its arguments will be set up wrongly (think hammering a square peg into a round hole); the consequences will be unpredictable.
In most cases you should be telling the compiler to treat such cases as an error. For example, on gcc, add -Wall -Werror to every compile line, or add them to the CFLAGS in a typical Makefile.
I just wrote a sample program in two separate files:
a1.c
#include <stdio.h>
int main() {
// using the external object
testing();
return 0;
}
which calls a function which exists in a2.c
void testing() {
printf("temp\n");
}
and then all I did was compile it using the following command:
$ gcc a1.c a2.c -o a1
and it Worked
Actually, when you compiled your file did you include the other C file (lineParser.c) in the compilation step to be parsed along with your ParseOne.c file?
In that case, what I understand is that, gcc would actually parse all the symbols in the associated C files (or obj files) and replace them appropriately in the final object file to be created.
Hope this helps.

Resources