Is my gcc configured right?

I've tried to install QT at least twice and the most basic of examples produce tons of errors. Now I've installed SFML and it acts the same way. I've tried the first of examples and I can't come close to compiling. I'll list the code from the example first. BTW, I'm doing this on Ubuntu Linux where I've never touched any settings of gcc and installed SFML using apt-get and accepting defaults.

int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}

window.clear();
window.draw(shape);
window.display();
}

return 0;
}

When trying to compile using the command gcc -o drawCircle main.cpp in the same folder as the source code, I get the following:
/usr/bin/ld: /tmp/cc6SwIIG.o: in function `main':
main.cpp:(.text+0x5c): undefined reference to `std::locale::locale()'
/usr/bin/ld: main.cpp:(.text+0x79): undefined reference to `sf::String::String(char const*, std::locale const&)'
/usr/bin/ld: main.cpp:(.text+0x97): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/usr/bin/ld: main.cpp:(.text+0xca): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/usr/bin/ld: main.cpp:(.text+0xe8): undefined reference to `std::locale::~locale()'
/usr/bin/ld: main.cpp:(.text+0x104): undefined reference to `sf::CircleShape::CircleShape(float, unsigned long)'
/usr/bin/ld: main.cpp:(.text+0x112): undefined reference to `sf::Color::Green'
/usr/bin/ld: main.cpp:(.text+0x11a): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
/usr/bin/ld: main.cpp:(.text+0x129): undefined reference to `sf::Window::isOpen() const'
/usr/bin/ld: main.cpp:(.text+0x14a): undefined reference to `sf::Window::pollEvent(sf::Event&)'
/usr/bin/ld: main.cpp:(.text+0x167): undefined reference to `sf::Window::close()'
/usr/bin/ld: main.cpp:(.text+0x18d): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
/usr/bin/ld: main.cpp:(.text+0x1aa): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
/usr/bin/ld: main.cpp:(.text+0x1c3): undefined reference to `sf::RenderStates::Default'
/usr/bin/ld: main.cpp:(.text+0x1ce): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
/usr/bin/ld: main.cpp:(.text+0x1dd): undefined reference to `sf::Window::display()'
/usr/bin/ld: main.cpp:(.text+0x205): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: main.cpp:(.text+0x246): undefined reference to `std::locale::~locale()'
/usr/bin/ld: main.cpp:(.text+0x27f): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: /tmp/cc6SwIIG.o: in function `std::__cxx11::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::_M_destroy(unsigned long)':
main.cpp:(.text._ZNSt7__cxx1112basic_stringIjSt11char_traitsIjESaIjEE10_M_destroyEm[_ZNSt7__cxx1112basic_stringIjSt11char_traitsIjESaIjEE10_M_destroyEm]+0x60): undefined reference to `__cxa_call_unexpected'
/usr/bin/ld: /tmp/cc6SwIIG.o: in function `__gnu_cxx::new_allocator<unsigned int>::deallocate(unsigned int*, unsigned long)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorIjE10deallocateEPjm[_ZN9__gnu_cxx13new_allocatorIjE10deallocateEPjm]+0x20): undefined reference to `operator delete(void*)'
/usr/bin/ld: /tmp/cc6SwIIG.o: in function `sf::CircleShape::~CircleShape()':
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x13): undefined reference to `vtable for sf::CircleShape'
/usr/bin/ld: main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x21): undefined reference to `vtable for sf::CircleShape'
/usr/bin/ld: main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x35): undefined reference to `sf::Shape::~Shape()'
/usr/bin/ld: /tmp/cc6SwIIG.o: in function `sf::CircleShape::~CircleShape()':
main.cpp:(.text._ZN2sf11CircleShapeD0Ev[_ZN2sf11CircleShapeD5Ev]+0x29): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/cc6SwIIG.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
You should use the g++ command rather than gcc when compiling C++ code. You are also missing the linking (-l) flags.

g++ -o drawCircle main.cpp -lsfml-graphics -lsfml-window -lsfml-system


https://www.sfml-dev.org/tutorials/2.5/start-linux.php
Last edited on
Peter87, first of all, thank you. Your instructions worked perfectly. The program compiled and linked and executed just as it should. I wonder if you would mind expanding a bit on what you told me. After all, those instructions are not going to work for everything and I'd like to know what you did so that I can use it in all my projects. Thank you again for the previous message and any further help you can provide.
Last edited on
When using a library it's quite common to have to add -l followed by the name of the library. In this case SFML is divided into multiple parts so that is why there are three of them. I don't know if you really need all of them for this particular program. If you use sound and networking you would also have to add -lsfml-audio and -lsfml-network. These are things that you'll have to look up with each library that you use.
Registered users can post here. Sign in or register to post.