2

I compiled the library SDL locally and I am trying to link it in a project. I am on OSX.

However I get stuck in the compilation with g++ when invoking a library function while it compiles correctly if I use only type variable from it (declarations).

I am compiling the following source code:

#include <iostream>
#include "SDL_keyboard.h"
#include "SDL_keycode.h"
#include "SDL_scancode.h"

#include "SDL.h"

using namespace std;

int main(int argc, char* argv []) {
  SDL_Event event;
  bool quit=false;
  while(!quit)
  {
      while(SDL_PollEvent(&event))
      {
          if(event.type==SDL_KEYDOWN)
          {
               cout<<"Key dow\n";
          }
          else if(event.type==SDL_QUIT)
          {
              quit=true;
          }
      }
  }
  return 0;
}

with the following g++ command:

g++ -I../../SDL2-2.0.8/build/include/SDL2/ -L../../SDL2-2.0.8/build/ -lSDL2 -lSDL2d `pkg-config --cflags --libs ../../SDL2-2.0.8/build/lib/pkgconfig/sdl2.pc` -lSDL2 testSDl.cpp -o test

and getting the undefined reference linking error:

Undefined symbols for architecture x86_64:
  "_AudioObjectAddPropertyListener", referenced from:
      _COREAUDIO_Init in libSDL2.a(SDL_coreaudio.m.o)
      _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
  "_AudioObjectGetPropertyData", referenced from:
      _COREAUDIO_OpenDevice in libSDL2.a(SDL_coreaudio.m.o)
      _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      _device_unplugged in libSDL2.a(SDL_coreaudio.m.o)
      _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
  "_AudioObjectGetPropertyDataSize", referenced from:
      _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
  "_AudioObjectRemovePropertyListener", referenced from:
      _COREAUDIO_CloseDevice in libSDL2.a(SDL_coreaudio.m.o)
      _COREAUDIO_Deinitialize in 

libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueAllocateBuffer", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueDispose", referenced from:
          _COREAUDIO_CloseDevice in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueEnqueueBuffer", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
          _inputCallback in libSDL2.a(SDL_coreaudio.m.o)
          _outputCallback in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueNewInput", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueNewOutput", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueSetProperty", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      "_AudioQueueStart", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
      "_CFArrayApplyFunction", referenced from:
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _AddHIDElement in libSDL2.a(SDL_sysjoystick.c.o)
      "_CFArrayCreate", referenced from:
          _SDL_SYS_JoystickInit in libSDL2.a(SDL_sysjoystick.c.o)
          _Cocoa_InitKeyboard in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFArrayGetCount", referenced from:
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _AddHIDElement in libSDL2.a(SDL_sysjoystick.c.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CFArrayGetValueAtIndex", referenced from:
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CFDataGetBytePtr", referenced from:
          _UpdateKeymap in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFDictionaryCreate", referenced from:
          _CreateHIDDeviceMatchDictionary in libSDL2.a(SDL_sysjoystick.c.o)
      "_CFDictionaryCreateMutable", referenced from:
          _CreateHIDDeviceMatchingDictionary in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFDictionaryGetValue", referenced from:
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
      "_CFDictionaryGetValueIfPresent", referenced from:
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
      "_CFDictionarySetValue", referenced from:
          _CreateHIDDeviceMatchingDictionary in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFGetTypeID", referenced from:
          _AddHIDElement in libSDL2.a(SDL_sysjoystick.c.o)
      "_CFNumberCreate", referenced from:
          _CreateHIDDeviceMatchDictionary in libSDL2.a(SDL_sysjoystick.c.o)
          _CreateHIDDeviceMatchingDictionary in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFNumberGetValue", referenced from:
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
      "_CFRelease", referenced from:
          _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
          _SDL_SYS_JoystickInit in libSDL2.a(SDL_sysjoystick.c.o)
          _SDL_SYS_JoystickQuit in libSDL2.a(SDL_sysjoystick.c.o)
          _CreateHIDDeviceMatchDictionary in libSDL2.a(SDL_sysjoystick.c.o)
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
          ...
      "_CFRunLoopGetCurrent", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
          _SDL_SYS_JoystickInit in libSDL2.a(SDL_sysjoystick.c.o)
          _FreeDevice in libSDL2.a(SDL_sysjoystick.c.o)
          _SDL_SYS_JoystickQuit in libSDL2.a(SDL_sysjoystick.c.o)
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
      "_CFRunLoopGetMain", referenced from:
          _Cocoa_InitKeyboard in libSDL2.a(SDL_cocoakeyboard.m.o)
      "_CFRunLoopRunInMode", referenced from:
          _audioqueue_thread in libSDL2.a(SDL_coreaudio.m.o)
          _SDL_SYS_JoystickInit in libSDL2.a(SDL_sysjoystick.c.o)
          _SDL_SYS_JoystickDetect in libSDL2.a(SDL_sysjoystick.c.o)
      "_CFStringCompare", referenced from:
          _SDL_GetPowerInfo_MacOSX in libSDL2.a(SDL_syspower.c.o)
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CFStringGetCString", referenced from:
          _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
      "_CFStringGetLength", referenced from:
          _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
      "_CFStringGetMaximumSizeForEncoding", referenced from:
          _build_device_list in libSDL2.a(SDL_coreaudio.m.o)
      "_CFStringGetSystemEncoding", referenced from:
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
      "_CFUUIDGetConstantUUIDWithBytes", referenced from:
          _SDL_SYS_HapticEffectType in libSDL2.a(SDL_syshaptic.c.o)
      "_CGAcquireDisplayFadeReservation", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGAssociateMouseAndMouseCursorPosition", referenced from:
          -[Cocoa_WindowListener mouseMoved:] in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_WarpMouseGlobal in libSDL2.a(SDL_cocoamouse.m.o)
          _Cocoa_SetRelativeMouseMode in libSDL2.a(SDL_cocoamouse.m.o)
      "_CGCaptureAllDisplays", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayBounds", referenced from:
          _Cocoa_GetDisplayBounds in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_GetDisplayUsableBounds in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayCapture", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayCopyAllDisplayModes", referenced from:
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayCopyDisplayMode", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayFade", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayIDToOpenGLDisplayMask", referenced from:
          _Cocoa_GL_CreateContext in libSDL2.a(SDL_cocoaopengl.m.o)
      "_CGDisplayIOServicePort", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayIsMain", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayMirrorsDisplay", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeCopyPixelEncoding", referenced from:
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeGetHeight", referenced from:
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeGetRefreshRate", referenced from:
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeGetWidth", referenced from:
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeRelease", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_QuitModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayModeRetain", referenced from:
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayMoveCursorToPoint", referenced from:
          -[Cocoa_WindowListener mouseMoved:] in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowGrab in libSDL2.a(SDL_cocoawindow.m.o)
      "_CGDisplayPixelsHigh", referenced from:
          -[Cocoa_WindowListener windowDidMove:] in libSDL2.a(SDL_cocoawindow.m.o)
          -[Cocoa_WindowListener windowDidResize:] in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_CreateWindow in libSDL2.a(SDL_cocoawindow.m.o)
          _SetupWindowData in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowPosition in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowSize in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowFullscreen in libSDL2.a(SDL_cocoawindow.m.o)
          ...
      "_CGDisplayPixelsWide", referenced from:
          _Cocoa_GetDisplayDPI in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayRelease", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplayScreenSize", referenced from:
          _Cocoa_GetDisplayDPI in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGDisplaySetDisplayMode", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGGetDisplayTransferByTable", referenced from:
          _Cocoa_GetWindowGammaRamp in libSDL2.a(SDL_cocoawindow.m.o)
      "_CGGetOnlineDisplayList", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGMainDisplayID", referenced from:
          -[Cocoa_WindowListener windowDidMove:] in libSDL2.a(SDL_cocoawindow.m.o)
          -[Cocoa_WindowListener windowDidResize:] in libSDL2.a(SDL_cocoawindow.m.o)
          -[Cocoa_WindowListener mouseMoved:] in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_CreateWindow in libSDL2.a(SDL_cocoawindow.m.o)
          _SetupWindowData in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowPosition in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowSize in libSDL2.a(SDL_cocoawindow.m.o)
          ...
      "_CGReleaseAllDisplays", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGReleaseDisplayFadeReservation", referenced from:
          _Cocoa_SetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CGSetDisplayTransferByTable", referenced from:
          _Cocoa_SetWindowGammaRamp in libSDL2.a(SDL_cocoawindow.m.o)
      "_CGShieldingWindowLevel", referenced from:
          _Cocoa_SetWindowFullscreen in libSDL2.a(SDL_cocoawindow.m.o)
          _Cocoa_SetWindowGrab in libSDL2.a(SDL_cocoawindow.m.o)
      "_CGWarpMouseCursorPosition", referenced from:
          _Cocoa_WarpMouseGlobal in libSDL2.a(SDL_cocoamouse.m.o)
      "_CVDisplayLinkCreateWithCGDisplay", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_CVDisplayLinkGetNominalOutputVideoRefreshPeriod", referenced from:
          _GetDisplayMode in libSDL2.a(SDL_cocoamodes.m.o)
      "_CVDisplayLinkRelease", referenced from:
          _Cocoa_InitModes in libSDL2.a(SDL_cocoamodes.m.o)
          _Cocoa_GetDisplayModes in libSDL2.a(SDL_cocoamodes.m.o)
      "_FFCreateDevice", referenced from:
          _SDL_SYS_HapticOpenFromService in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceCreateEffect", referenced from:
          _SDL_SYS_HapticNewEffect in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceGetForceFeedbackCapabilities", referenced from:
          _SDL_SYS_HapticOpenFromService in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceGetForceFeedbackProperty", referenced from:
          _SDL_SYS_HapticOpenFromService in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceReleaseEffect", referenced from:
          _SDL_SYS_HapticDestroyEffect in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceSendForceFeedbackCommand", referenced from:
          _SDL_SYS_HapticOpenFromService in libSDL2.a(SDL_syshaptic.c.o)
          _SDL_SYS_HapticPause in libSDL2.a(SDL_syshaptic.c.o)
          _SDL_SYS_HapticUnpause in libSDL2.a(SDL_syshaptic.c.o)
          _SDL_SYS_HapticStopAll in libSDL2.a(SDL_syshaptic.c.o)
      "_FFDeviceSetForceFeedbackProperty", referenced from:
          _SDL_SYS_HapticSetGain in libSDL2.a(SDL_syshaptic.c.o)
          _SDL_SYS_HapticSetAutocenter in libSDL2.a(SDL_syshaptic.c.o)
      "_FFEffectGetEffectStatus", referenced from:
          _SDL_SYS_HapticGetEffectStatus in libSDL2.a(SDL_syshaptic.c.o)
      "_FFEffectSetParameters", referenced from:
          _SDL_SYS_HapticUpdateEffect in libSDL2.a(SDL_syshaptic.c.o)
      "_FFEffectStart", referenced from:
          _SDL_SYS_HapticRunEffect in libSDL2.a(SDL_syshaptic.c.o)
      "_FFEffectStop", referenced from:
          _SDL_SYS_HapticStopEffect in libSDL2.a(SDL_syshaptic.c.o)
      "_FFIsForceFeedback", referenced from:
          _JoystickDeviceWasAddedCallback in libSDL2.a(SDL_sysjoystick.c.o)
          _MacHaptic_MaybeAddDevice in libSDL2.a(SDL_syshaptic.c.o)
      "_FFReleaseDevice", referenced from:
          _SDL_SYS_HapticOpenFromService in libSDL2.a(SDL_syshaptic.c.o)
          _SDL_SYS_HapticClose in libSDL2.a(SDL_syshaptic.c.o)
      "_IODisplayCreateInfoDictionary", referenced from:
          _Cocoa_InitModes in 

[The error continues but it is too long]

I tried this solution but it is not working

roschach
  • 8,390
  • 14
  • 74
  • 124
  • [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568) – Mike Kinghan Aug 30 '18 at 13:23
  • Why has it been marked as a duplicate of that question that is so general? What answer should I look at? The other question is asking in general what is a linking error (by the way general questions are now discouraged on SO) while I am asking what is generating the my specific linkage error with this library. – roschach Aug 30 '18 at 13:41
  • 1
    You should look at the answer to which I posted the link above. – Mike Kinghan Aug 30 '18 at 13:42

0 Answers0