38

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&)' 
6
  • 1
    You are linking against your C++ standard library, right? Commented Jun 21, 2010 at 2:34
  • You're getting linker errors. Are all the libraries involved using the same version of the same compiler, using the same runtime settings? Commented Jun 21, 2010 at 2:35
  • Can you paste the command that you're using to compile? Commented Jun 21, 2010 at 2:35
  • 4
    You need to post more context to your question. What, for example, is contained in Particle_Engine.h (post the code), or is it a third-party header? Also, you should tell us the entire command you're using when compiling. Commented Jun 21, 2010 at 3:36
  • 1
    You shouldn't change a question to something totally else. Accept an appropriate answer to your original question and then start a new question with your new problems. I went in and did a rollback to your original question so that the answers still make sense. Commented Jun 21, 2010 at 6:30

2 Answers 2

97

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++.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the consideration of gcc to leave it out?
@xji gcc compiles C, g++ compiles C++. Your code is probably C++ so that's why it doesn't work with gcc.
33

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 

1 Comment

This seems to be a more valid answer. Was the same for clang as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.