I've been getting this undefined symbol building with this command line:
$ gcc test.cpp
Undefined symbols:
"___gxx_personality_v0", referenced from:
etc...
test.cpp is simple and should build fine. What is the deal?
Use
g++ test.cpp
instead, since this is c++ code.
Or, if you really want to use gcc, add -lstdc++ to the command line, like so:
gcc test.cpp -lstdc++
Running md5 against the a.out produced under each scenario shows that it's the same output.
But, yeah, g++ probably makes your world a simpler place.
The .cpp extension causes gcc to compile your file as a C++ file. (See the GCC docs.)
Try compiling the same file, but rename it to have a .c extension:
mv test.cpp
gcc test.c
Alternatively, you can explicitly specify the language by passing -x c to the compiler:
gcc -x c -c test.cpp -o test.o
If you run nm test.o on these C-language versions, you'll notice that ___gxx_personality_v0 is not listed as a symbol.
(And if you run the same command on an object file generated with gcc -c test.cpp -o test.o, the ___gxx_personality_v0 symbol is present.)
Just in case anyone has the same problem as me: The file extension should be a .c not a .C (gcc is case-sensitive).
Had the same problem, but a different solution:
C++ code in static library getting linked, and being referenced by a .m file. Renaming the .m file to .mm fixed the issue.
Related
I've been getting this undefined symbol building with this command line:
$ gcc test.cpp
Undefined symbols:
"___gxx_personality_v0", referenced from:
etc...
test.cpp is simple and should build fine. What is the deal?
Use
g++ test.cpp
instead, since this is c++ code.
Or, if you really want to use gcc, add -lstdc++ to the command line, like so:
gcc test.cpp -lstdc++
Running md5 against the a.out produced under each scenario shows that it's the same output.
But, yeah, g++ probably makes your world a simpler place.
The .cpp extension causes gcc to compile your file as a C++ file. (See the GCC docs.)
Try compiling the same file, but rename it to have a .c extension:
mv test.cpp
gcc test.c
Alternatively, you can explicitly specify the language by passing -x c to the compiler:
gcc -x c -c test.cpp -o test.o
If you run nm test.o on these C-language versions, you'll notice that ___gxx_personality_v0 is not listed as a symbol.
(And if you run the same command on an object file generated with gcc -c test.cpp -o test.o, the ___gxx_personality_v0 symbol is present.)
Just in case anyone has the same problem as me: The file extension should be a .c not a .C (gcc is case-sensitive).
Had the same problem, but a different solution:
C++ code in static library getting linked, and being referenced by a .m file. Renaming the .m file to .mm fixed the issue.
I want to use OpenGL GLEW library. I have the binary downloaded and its folder is in the folder with my .cpp file. My .cpp file uses #include <eglew.h>.
How should I format my command for MinGW to compile my .cpp file? Do I compile with the .lib file like g++ -L./path/to/lib/file.lib test.cpp -o test or do I do something else like link to the header files g++ -I./path/to/headers test.cpp -o test?
To better understand things maybe it's better to split compiling and linking steps.
If you get errors then you will also know in which step the problem occurs.
I'm assuming you have the following folders/files:
/path/to/eglew/include/GL/eglew.h
/path/to/eglew/lib/libglew32.a
Compiling:
g++ -Wall -c -o test.o test.cpp -I/path/to/eglew/include/GL
Linking:
g++ -o test.exe test.o -L/path/to/eglew/lib -lglew32
Though I would expect to see #include <GL/eglew.h> in which case the linker include flag should be -I/path/to/eglew/include.
I've been getting this undefined symbol building with this command line:
$ gcc test.cpp
Undefined symbols:
"___gxx_personality_v0", referenced from:
etc...
test.cpp is simple and should build fine. What is the deal?
Use
g++ test.cpp
instead, since this is c++ code.
Or, if you really want to use gcc, add -lstdc++ to the command line, like so:
gcc test.cpp -lstdc++
Running md5 against the a.out produced under each scenario shows that it's the same output.
But, yeah, g++ probably makes your world a simpler place.
The .cpp extension causes gcc to compile your file as a C++ file. (See the GCC docs.)
Try compiling the same file, but rename it to have a .c extension:
mv test.cpp
gcc test.c
Alternatively, you can explicitly specify the language by passing -x c to the compiler:
gcc -x c -c test.cpp -o test.o
If you run nm test.o on these C-language versions, you'll notice that ___gxx_personality_v0 is not listed as a symbol.
(And if you run the same command on an object file generated with gcc -c test.cpp -o test.o, the ___gxx_personality_v0 symbol is present.)
Just in case anyone has the same problem as me: The file extension should be a .c not a .C (gcc is case-sensitive).
Had the same problem, but a different solution:
C++ code in static library getting linked, and being referenced by a .m file. Renaming the .m file to .mm fixed the issue.
I've been getting this undefined symbol building with this command line:
$ gcc test.cpp
Undefined symbols:
"___gxx_personality_v0", referenced from:
etc...
test.cpp is simple and should build fine. What is the deal?
Use
g++ test.cpp
instead, since this is c++ code.
Or, if you really want to use gcc, add -lstdc++ to the command line, like so:
gcc test.cpp -lstdc++
Running md5 against the a.out produced under each scenario shows that it's the same output.
But, yeah, g++ probably makes your world a simpler place.
The .cpp extension causes gcc to compile your file as a C++ file. (See the GCC docs.)
Try compiling the same file, but rename it to have a .c extension:
mv test.cpp
gcc test.c
Alternatively, you can explicitly specify the language by passing -x c to the compiler:
gcc -x c -c test.cpp -o test.o
If you run nm test.o on these C-language versions, you'll notice that ___gxx_personality_v0 is not listed as a symbol.
(And if you run the same command on an object file generated with gcc -c test.cpp -o test.o, the ___gxx_personality_v0 symbol is present.)
Just in case anyone has the same problem as me: The file extension should be a .c not a .C (gcc is case-sensitive).
Had the same problem, but a different solution:
C++ code in static library getting linked, and being referenced by a .m file. Renaming the .m file to .mm fixed the issue.
I have a requirement of creating a C++ program which exposes certain functions through HTTP. For that reason I was trying to use libmicrohttpd for the same. Now this library is written in C. However I am kind of new to C++ and am trying to compile this C and C++ code given here. (Which can be git cloned from here)
Now I need help in understanding how g++ may be used to compile a program which is not written completely in C++. And/or how to compile the above linked code.
PS: Working in linux
And finally if someone can point to an easier alternative than libmicrohttpd - I am all ears.
Edit to Edit:
Finally got it working. Compiled the individual cpp files with gcc and then linked everything using g++. I have no clue how this came to work, maybe some one can reply below.
I have made the following script to compile and link:
LOC="path/to/directory"
gcc -c httphandler.cpp -o httphandler.o -I $LOC
gcc -c strutil.cpp -o strutil.o -I $LOC
gcc -c api.cpp -o api.o -I $LOC
gcc -c executor.cpp -o executor.o -I $LOC
g++ -o out httphandler.o strutil.o api.o executor.o -lmicrohttpd -lboost_regex
But in the final step I am getting the following error:
/usr/bin/ld: strutil.o: undefined reference to symbol '__cxa_free_exception##CXXABI_1.3'
/usr/bin/ld: note: '__cxa_free_exception##CXXABI_1.3' is defined in DSO /usr/lib/x86_64-linux-gnu/libstdc++.so.6 so try adding it to the linker command line
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: could not read symbols: Invalid operation
What gives?
For starters, don't compile the C code with g++, use gcc instead. Then just include the header file and use the functions normally. When linking don't forget to link with the object file(s) generated from compiling the libmicrohttpd source file(s).