Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ errors while compiling

Tags:

c++

gcc

I am trying to compile a game, but getting 100+ errors like:

C:\Users\AppData\Local\Temp\cctQCagR.o: In function `load_image(std::string)': main.cpp:(.text+0x4bd4): undefined reference to `std::string::c_str() const' C:\Users\Bill\AppData\Local\Temp\cctQCagR.o: In function `createShip(float, float)': main.cpp:(.text+0x4da4): undefined reference to `std::allocator<char>::allocator()' main.cpp:(.text+0x4dbc): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons t&)' main.cpp:(.text+0x4de4): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::~basic_string()' main.cpp:(.text+0x4e04): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::~basic_string()' main.cpp:(.text+0x4e1c): undefined reference to `std::allocator<char>::~allocator()' main.cpp:(.text+0x4e28): undefined reference to `std::allocator<char>::allocator()' main.cpp:(.text+0x4e40): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons t&)' main.cpp:(.text+0x4e60): undefined reference to `std::allocator<char>::~allocator()' main.cpp:(.text+0x4e70): undefined reference to `__cxa_end_cleanup' main.cpp:(.text+0x4e98): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::~basic_string()' main.cpp:(.text+0x4eb8): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::~basic_string()' main.cpp:(.text+0x4ed0): undefined reference to `std::allocator<char>::~allocator()' main.cpp:(.text+0x4ef4): undefined reference to `std::allocator<char>::~allocator()' main.cpp:(.text+0x4f04): undefined reference to `__cxa_end_cleanup' C:\Users\Bill\AppData\Local\Temp\cctQCagR.o: In function `load_files()': main.cpp:(.text+0x5164): undefined reference to `std::allocator<char>::allocator()' main.cpp:(.text+0x517c): undefined reference to `std::basic_string<char, std::char_tra its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons t&)' 
like image 842
jack moore Avatar asked Jun 21 '10 02:06

jack moore


People also ask

Where are compile-time errors in C?

Compile-time errors are generally referred to the error corresponding to syntax or semantics. Runtime errors on the other hand refer to the error encountered during the execution of code at runtime. Compile-time errors get detected by compiler at the time of code development.

What are the 3 types of programming errors?

When developing programs there are three types of error that can occur: syntax errors. logic errors. runtime errors.

What are syntax errors during compiling?

Syntax errors are mistakes in the source code, such as spelling and punctuation errors, incorrect labels, and so on, which cause an error message to be generated by the compiler. These appear in a separate error window, with the error type and line number indicated so that it can be corrected in the edit window.


2 Answers

I believe you're trying to compile main.cpp with gcc instead of g++.

#include <string> #include <stdio.h> int main() {     std::string bla;     bla = "BLA BLA";     printf("%s\n",bla.c_str());     return 0; } 

If you build the above code snippet with gcc you get the errors you mention. If you use g++ it build ok, this makes sense since g++ will make sure all the proper stuff it put together when build C++.

like image 101
ntroncos Avatar answered Oct 16 '22 03:10

ntroncos


You need to link your binary with libstdc++. You need to explicitly specify it in command line if using gcc:

gcc -lstdc++ tmp.cpp 

If using g++, libstdc++ will be linked by default:

g++ tmp.cpp 
like image 44
ks1322 Avatar answered Oct 16 '22 05:10

ks1322