0

I am getting this "symbol(s) not found in architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)." Actually, these messages didn't appear until I upgraded to macOS Mojave.

The complete message I got is:

g++ -Wall -fexceptions -g  -c /Users/suraj/Desktop/sfm/main.cpp -o obj/Debug/main.o
g++  -o bin/Debug/sfm obj/Debug/main.o   
Undefined symbols for architecture x86_64:
"sf::CircleShape::CircleShape(float, unsigned long)", referenced from:
  _main in main.o
"sf::RenderStates::Default", referenced from:
  _main in main.o
"sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)", 
referenced from:
  _main in main.o
"sf::RenderTarget::clear(sf::Color const&)", referenced from:
  _main in main.o
"sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, 
 unsigned int, sf::ContextSettings const&)", referenced from:
  _main in main.o
"sf::RenderWindow::~RenderWindow()", referenced from:
  _main in main.o
"sf::Color::Green", referenced from:
  _main in main.o
"sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned 
char)", referenced from:
  _main in main.o
"sf::Shape::setFillColor(sf::Color const&)", referenced from:
  _main in main.o
"sf::Shape::~Shape()", referenced from:
  sf::CircleShape::~CircleShape() in main.o
"sf::String::String(char const*, std::__1::locale const&)", referenced 
from:
  _main in main.o
"sf::Window::close()", referenced from:
  _main in main.o
"sf::Window::display()", referenced from:
  _main in main.o
"sf::Window::pollEvent(sf::Event&)", referenced from:
  _main in main.o
"sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)", 
referenced from:
  _main in main.o
"sf::Window::isOpen() const", referenced from:
  _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The code is:

#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(400, 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;
}

Please help.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
suraj gaire
  • 1
  • 1
  • 2
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – melpomene Nov 28 '18 at 00:30
  • The actual error message will tell you exactly (and often with brutal, soul-crushing detail that goes on for pages and pages) what was missing. We need to see that actual error message (or the first few entries at least should it go on for pages and pages) to be able to offer much help to you. – user4581301 Nov 28 '18 at 00:38
  • I have posted the complete message now. – suraj gaire Nov 28 '18 at 00:55
  • Thank you. It also included the build commands which were even more useful. In `g++ -o bin/Debug/sfm obj/Debug/main.o` there should be also be a `-L`, `-lsfml-graphics`, a `-lsfml-window` and probably a `-lsfml-system`. Sadly I don't know what build tool you're using so I can't recommend how best to add them. – user4581301 Nov 28 '18 at 01:02
  • I have no Mad Macs skills, but perhaps https://www.sfml-dev.org/tutorials/2.5/start-osx.php will help. – user4581301 Nov 28 '18 at 01:03
  • Did you install `sfml` using **homebrew**? – Mark Setchell Nov 28 '18 at 01:06
  • yes i installed sfml with homebrew – suraj gaire Nov 28 '18 at 02:32

1 Answers1

2

Updated Answer - May 2019

It seems that the openal package is no longer in homebrew and that causes the instructions below to fail. I don't know the background behind it. Here is another approach.

You can find all the pkg-config related parts of sfml like this:

find /usr/local/Cellar/sfml -name \*pc

/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-network.pc
/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-all.pc
/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-graphics.pc
/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-audio.pc
/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-system.pc
/usr/local/Cellar/sfml/2.5.1/lib/pkgconfig/sfml-window.pc

It seems you cannot use sfml-all nor sfml-audio in the above list, so you will need to pick which parts you want to use and grab them individually, so if you want graphics, window and system:

pkg-config --libs --cflags sfml-graphics
-I/usr/local/Cellar/sfml/2.5.1/include 
-I/usr/local/opt/freetype/include/freetype2 
-I/usr/local/Cellar/sfml/2.5.1/include 
-L/usr/local/Cellar/sfml/2.5.1/lib -lsfml-graphics -lsfml-window -lsfml-system

pkg-config --libs --cflags sfml-system
-I/usr/local/Cellar/sfml/2.5.1/include 
-L/usr/local/Cellar/sfml/2.5.1/lib -lsfml-system

pkg-config --libs --cflags sfml-window
-I/usr/local/Cellar/sfml/2.5.1/include
-L/usr/local/Cellar/sfml/2.5.1/lib -lsfml-window -lsfml-system

So, you would compile with:

g++ main.cpp $(pkg-config --libs --cflags sfml-window sfml-system sfml-graphics) -o main

Original Answer

If you installed sfml via homebrew, I would suggest you also install pkg-config like this:

brew install pkg-config

Then you can get the switches needed to compile with:

pkg-config --libs --cflags sfml-all

Sample Output

-I/usr/local/Cellar/sfml/2.4.2_1/include -L/usr/local/Cellar/sfml/2.4.2_1/lib -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-network -lsfml-system

So you can compile with:

g++ main.cpp $(pkg-config --libs --cflags sfml-all) -o main
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • None of these commands worked for me, any alternative solutions? – Eddy Ekofo May 03 '19 at 05:59
  • @EddyEkofo I had another look and updated my answer. If that doesn't work, please open a new question (questions are free) with all the diagnostics and error messages and add a note here linking to it - get the link by clicking `share` under your new question and copying the link. – Mark Setchell May 03 '19 at 07:44
  • here is the Qs: [https://stackoverflow.com/questions/55973407/fatal-error-sfml-system-hpp-file-not-found ]. Any help would be appreciated. – Eddy Ekofo May 03 '19 at 15:58