0

I'm trying to compile using SDL2 library with no success.

OS: MacOS 10.13.2
Visual Studio Code 1.43.1
Language: c++

I'm using the Lazy Foo's very basic example code. This is the message I recieve:

[Running] cd "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/" && g++ 01_hello_SDL.cpp -o 01_hello_SDL && "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/"01_hello_SDL
Undefined symbols for architecture x86_64:
  "_SDL_CreateWindow", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Delay", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_DestroyWindow", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_FillRect", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_GetError", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_GetWindowSurface", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Init", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_MapRGB", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Quit", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_UpdateWindowSurface", referenced from:
      _main in 01_hello_SDL-a16d96.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

[Done] exited with code=1 in 0.168 seconds
stark
  • 12,615
  • 3
  • 33
  • 50

1 Answers1

1

Building C++ code requires three stages:

Preprocessor deals with macros and #include directives, so your header files are required at this step.

Compiler generates object files. This is the point when the syntax is checked in your source files.

Linker combines object files and libraries into an executable so your runtime libraries must be available and specified to the linker.

For g++, you specify libraries by giving their full name on the command line or use the -l shortcut which figures out the proper prefix (lib) and suffix. So in your case I believe you can add -lSDL.

EDIT: If that doesn't work, check the output of

sdl-config --libs
stark
  • 12,615
  • 3
  • 33
  • 50
  • 1
    I recieve this error (I've tried with different names -lSDL -lSDL2): "library not found for -lSDL2 clang: error: linker command failed with exit code 1 (use -v to see invocation) The terminal process terminated with exit code: 1" – Martin Lizondo Mar 24 '20 at 18:07