1

I'm building a project in C++ that requires calling an external library. In particular, I need access to a singleton which is created in a static method of an external class. However, when I try to make it with CMake, I get a linker error:
undefined reference to `AVT::VmbAPI::VimbaSystem::GetInstance()'
I'm sure the path is right, as the code compiles and runs without issue if I don't call GetInstance().

The code itself is pretty trivial, so I think this must be an issue with my CMake configuration. The relevant files are below:

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)
PROJECT(Thing LANGUAGES CXX)

#set(CMAKE_VERBOSE_MAKEFILE ON)

SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-Wall -Wextra -Wno-deprecated -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
-Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual -Wpedantic \
-ffast-math \
-fPIC \
")

ENABLE_TESTING()

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(test)

src/CMakeLists.txt

AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)

ADD_EXECUTABLE(thing ${source})

TARGET_LINK_LIBRARIES(thing LINK_PUBLIC Camera)

lib/CMakeLists.txt

ADD_LIBRARY(Camera ${CMAKE_CURRENT_SOURCE_DIR}/camera.cpp)

TARGET_INCLUDE_DIRECTORIES(Camera
    PUBLIC
        ../include
        ${CMAKE_CURRENT_SOURCE_DIR}}
        /opt/Vimba_2_0
        /opt/Vimba_2_0/VimbaCPP/Include
)

src/main.cpp

#include <camera.h>

int main() {
    Camera c = Camera();

    return 0;
}

lib/camera.cpp

#include "../include/camera.h"

Camera::Camera() {
    AVT::VmbAPI::VimbaSystem &sys = AVT::VmbAPI::VimbaSystem::GetInstance();
}

include/camera.h

#ifndef CAMERA_H
#define CAMERA_H

#include "VimbaCPP.h"
#include "VimbaSystem.h"

class Camera {
public:
    Camera();
    ~Camera() = default;
};

#endif //CAMERA_H
  • A complete minimal example would be better. I can't test your code otherwise. Do you also get an error with dynamic linking? – Raul Laasner May 31 '18 at 17:51
  • Does it work if you try to build without cmake (using the same flags and linking against the same libraries)? – Stephen Newell May 31 '18 at 17:58
  • @StephenNewell Using the build command that cmake uses gives me the same error, even when I add in the include dirs and link dirs manually. I'm not sure if I'm using the right command. –  May 31 '18 at 18:26
  • Well, so the question nothing more than common "undefined reference" problem - you use a symbol (static function in your case) which is never defined in your code. – Tsyvarev May 31 '18 at 20:37
  • @Tsyvarev Yes, that does seem to be the case. My IDE figured out where it was anyway, so I assumed it was defined properly. –  May 31 '18 at 20:43
  • Note: `target_link_libraries` + `LINK_PUBLIC` is deprecated since *CMP0022*. Should be expanded in links and dependencies link... – Sandburg Jun 25 '21 at 08:37

1 Answers1

1

Apparently, there was a dynamic library I needed to link that wasn't mentioned in the Vimba docs. For anyone running into this issue in the future, I solved it by adding the following line to lib/CMakeLists.txt.

TARGET_LINK_LIBRARIES(Camera /opt/Vimba_2_0/VimbaCPP/DynamicLib/x86_64bit/libVimbaCPP.so)