I wrote the simple program in linux ubuntu, when I use g++ there is no error but when I use gcc I see this error:
test.c:1:17: fatal error: cmath: No such file or directory #include <cmath>
Note : "as a matter of fact I see this error in compiling the package, I thought it might be related to gcc library which is not set to linux environment, so I wrote the simple program to determine the error clearly and whitout dependency!"
so the program should compile with gcc so that I can over come the main problem.
I khow that I can use math.h instead of cmath, but the packege used the cmath!
this is the simple program:
/*test.c*/
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(){
double sinx = sin(3.14/3);
cout<< "sinx= " << sinx;
return 0;
}
here is cmath pathes:
root#geant4:/# find -name cmath
./opt/root5.32.00/cint/cint/include/cmath
./app/gcc/4.8.0/include/c++/4.8.0/ext/cmath
./app/gcc/4.8.0/include/c++/4.8.0/cmath
./app/gcc/4.8.0/include/c++/4.8.0/tr1/cmath
./usr/include/boost/compatibility/cpp_c_headers/cmath
./usr/include/boost/tr1/tr1/cmath
./usr/include/c++/4.5/cmath
./usr/include/c++/4.5/tr1_impl/cmath
./usr/include/c++/4.5/tr1/cmath
./usr/include/c++/4.6/cmath
./usr/include/c++/4.6/tr1/cmath
./usr/share/gccxml-0.9/GCC/2.95/cmath
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/cmath
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/cmath
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/include/ext/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c_global/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c_std/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/include/tr1/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/testsuite/26_numerics/headers/cmath
./gcc-build/gcc-4.8.0/libstdc++-v3/testsuite/tr1/8_c_compatibility/cmath
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/cmath
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath
and after installing gcc-4.8 I did this instruction:
root#geant4:~/Desktop# update-alternatives --install /usr/bin/gcc gcc /app/gcc/4.8.0/bin/gcc 40 --slave /usr/bin/g++ g++ /app/gcc/4.8.0/bin/g++
root#geant4:~/Desktop#update-alternatives --install /usr/bin/gcc gcc /app/gcc/4.8.0/bin/gcc 60 --slave /usr/bin/g++ g++ /app/gcc/4.8.0/bin/g++
root#geant4:~/Desktop# update-alternatives --config gcc
to make gcc-4.8 my default gcc.
now
root#geant4:~/Desktop# gcc --version
gcc (GCC) 4.8.0
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
as a matter of fact I wrote the main problem in https://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath
please help me
I don`t know what to do.
thanks
Some basics::
GCC:: GNU Compiler Collection
G++:: GNU C++ Compiler
Both are drivers which calls the compilers as needed.
Clearing your doubt::
The problem with GCC is that it doesn't links in the std C++ libraries by default as G++ does. GCC is just a front-end. The actual compiler is cc1plus. So it is always advisable to use G++ when compiling C++ files. The result can be same with both GCC and G++ if you do know the exact arguments to link them. You may find this link helpful.
But if you still want to use GCC, use it with linker-option -lstdc++ at the end of the command. This linker-option is added by default when you use G++. You can verify this by compiling your code using GCC with -### option and it will show you that -lstdc++ option is missing.
Compile C++ source files with g++, not gcc.
Related
I'm currently trying to compile my own gcc 9.1.0 cross compiler for aarch64-linux-gnu target. I used this tutorial: https://wiki.osdev.org/GCC_Cross-Compiler
The compile progress for the gcc and g++ compiler seems to finish without errors, but allways when I try to compile libgcc with the command make all-target-libgcc I run into this error:
In file included from ../../../gcc-9.1.0/libgcc/gthr.h:148,
from ../../../gcc-9.1.0/libgcc/libgcov-interface.c:27:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
35 | #include <pthread.h>
| ^~~~~~~~~~~
compilation terminated.
g++ --version on my build maschine prints:
g++ (GCC) 9.1.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
And my configuration command for gcc is:
../gcc-9.1.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --without-headers
With:
export TARGET=aarch64-linux-gnu
export PREFIX=/opt/aarch64-linux-gnu
What do I forget?
From GCC installation manual:
In order to build GCC, the C standard library and headers must be
present for all target variants for which target libraries will be
built (and not only the variant of the host C++ compiler).
So you need a libc for aarch64.
If you want to build it yourself, Preshing has a good article on the topic, which I haven't read in full but recommend anyway. He even has a picture mentioning both pthread.h and libgcc.a.
AFAIK, GCC can pick up a symlink to newlib sources and build it automatically during the course of building a full cross compiler with target libraries. Not sure about Glibc.
I follow my tutorial in linux to install some package:
yum install -y pcre pcre-devel openssl openssl-devel gcc gcc++
I am not sure about the gcc means GPN Compiler Collection or GPN C Compiler, I just not sure, because if it means the latter, the gcc++ maybe means GNU Compiler C++.
Who is there to answer my doubts?
gcc is GCC and gcc++ is G++ which are both GNU compilers
GCC is GNU’s C Compiler, and
G++ is GNU’s C++ Compiler
The difference is:
GCC will compile both .c and .cpp files. However, it will treat .c files as C programs and .cpp files as C++ programs.
G++ will also compile both .c and .cpp files just like the GCC compiler. The difference is that it will treat both .c and .cpp files as C++ programs.
From the "Programming Languages Supported by GCC" page:
The abbreviation GCC has multiple meanings in common use. The current official meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools. The name historically stood for “GNU C Compiler”, and this usage is still common when the emphasis is on compiling C programs. Finally, the name is also used when speaking of the language-independent component of GCC: code shared among the compilers for all supported languages.
And regarding the difference between the gcc and g++ commands, see the "GCC Command Options" page:
The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead. See Compiling C++ Programs, for information about the differences in behavior between gcc and g++ when compiling C++ programs.
and from "Compiling C++ Programs"
the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats ‘.c’, ‘.h’ and ‘.i’ files as C++ source files instead of C source files unless -x is used.
my program compiled and worked fine with:
g++ main.cpp exm1.cpp exm2.cpp -o main.o
i want to compile this app and run anywhere
how can i?
i try this code
g++ -g -Wall -I/MyApp/lib -static-libgcc -static-libstdc++ -static main.cpp exm1.cpp exm2.cpp -o main.o
but not work
in lib folder has 2 files:
exm1.h
exm2.h
main.cpp included:
#include <fstream>
#include <iostream>
#include <string>
#include <streambuf>
#include <stdlib.h>
#include "lib/exm1.h"
#include "lib/exm2.h"
my linux is kali, and i want run this app on CentOS 6
please help me,thanks
As C. bear said, use the -m32 flag so the program is able to run on 64- and 32- bit systems. In doing this, You'll also have to install the 32bit stdlibs. I'm not sure how to do this on kali, give it a google. Another thing you can do to avoid having users install external libraries (if you use them at any point) is to statically link you executable. Use the -static flag to do this. However, because all your app's dependencies (including the stdlibs) will be in a single file, it will get pretty big. On the other hand, loading times will be better, because your app doesn't have to run the dynamic linker and wait for it to link your external libraries as you call them.
So from comments one reason could be you executable is 64-bit and centOS a 32-bit sytem. Try compilation with -m32 flag.
how to compile g++ app and run anywhere?
It is not easily possible. For example, an ELF executable produced by GCC on a recent Debian/x86-64 computer won't run on a RaspberryPi (with a linux for ARM 32 bits, e.g. some Raspbian)
Read more about execve(2), elf(5) and about fat binaries
Be aware that the GCC compiler suite can be built (from its source code) as a cross-compiler (but you'll need to rebuild GCC for every different target system).
I recommend to invoke GCC as g++ -Wall -Wextra -g during the debugging phase (and use GDB). Once your software is debugged, compile it with g++ -Wall -Wextra -O2 to get an optimized binary (or even compile and link it with g++ -Wall -Wextra -O3 -flto)
First I install sctp on Ubuntu 12.04
sudo apt-get install libsctp-dev lksctp-tools
Then in my .c file,I include :
#include < netinet/in.h >
#include < netinet/sctp.h >
#include < sys/socket.h >
#include < stdlib.h >
#include < unistd.h >
howerver,when I compiled with gcc,the result is:
undefined reference to `sctp_recvmsg'
undefined reference to `sctp_get_no_strms'
undefined reference to `sctp_sendmsg'
What is wrong?
If you really compile with gcc temp.c -o temp then you are not linking any libraries (except the default libc.6.so), and you need some additional argument to gcc ; perhaps try to compile with
gcc -Wall -g temp.c -lsctp -o temp
Once your program is debugged with the help of the gdb debugger and you consider it to be bug-free, you may ask the compiler to optimize it using
gcc -Wall -O2 temp.c -lsctp -o temp
The order of program arguments to gcc is important and significant.
centos: yum install lksctp-tools-devel
I have some trouble with the placement of the -l option when using gcc. Here's a stripped down version for reproduce the problem.
t.c:
#include <pthread.h>
int main() {
pthread_create(0, 0, 0, 0);
}
and in terminal:
$ gcc -lpthread t.c
/tmp/ccmkwV7B.o: In function `main':
t.c:(.text+0x29): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
$ gcc t.c -lpthread
$ (compiles ok)
Why do I have to put -lpthread in the end to make it work? And it seems that this problem only occurs on 32bit linux.
My environment info is attached below:
gcc -lpthread t.c fails on this machine.
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
$ uname -rm
3.0.0-12-generic i686
gcc -lpthread t.c works on this machine.
$ uname -rm
2.6.18-274.3.1.el5 x86_64
$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
I looked up the gcc manual, and it says that "the placement of -l is significant". What exactly does it mean?
From the manual,
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
This means it is very interesting that linking the library first works on gcc 4.1.2. This might have to do with the default libraries linked to by the compiler. I know on some installations I don't need to explicitly link to pthreads.
On further reflection, I think the issue is with the flag --as-needed, which may be on by default in your gcc 4.6 system. See this link for some discussion.