Undefine Symbols vtkImageViewer2::New(), QT-VTK Mac OSX 10.6.8 - c++

I'm using VTK to read a DICOM series. I can compile (using CMake) VTK example code and it runs well. Now, I'm trying to use Qtcreator and Qt4.8.5 to create a GUI. I have linked the library and include path on project.pro.
When I build, I get:
Undefined symbols:
"vtkImageViewer2::New()", referenced from:
vtkSmartPointer<vtkImageViewer2>::New() in mainwindow.o
ld: symbol(s) not found
I checked, the Include path, and it includes /usr/local/vtk-6.1/include/vtk-6.1/
which contains vtkImageViewer2.h.
What's wrong with my project?

That is a linker error, so your project is indeed finding vtkImageViewer2.h correctly. You should use CMake to create your project, then you can simply do
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
and all of the VTK linking will be taken care of for you.

As an addendum to David's answer, do not forgot to add ${VTK_LIBRARIES} (defined by the VTK Use file) to your library or binary/executable:
TARGET_LINK_LIBRARIES(myLib ${VTK_LIBRARIES})
and the paths to the libraries as:
link_directories(${VTK_LIBRARY_DIRS})

Related

Can't link libraries for C++11 on MacOS Mojave

I'm having trouble getting boost/openssl to link to my C++ project in CLion. I'm trying to build my program in CLion by clicking build, although I've also been trying to just manually compile with g++ but to no avail. My CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.12)
project(FinalProject)
set(CMAKE_CXX_STANDARD 11)
# Set OpenSSL dir, this should be default on linux/mac
set(OPENSSL_INCLUDE_DIR, /usr/local/opt/openssl/bin/openssl)
# Get OpenSSL
find_package(openssl REQUIRED)
# Get cppRestSDK
find_package(cpprestsdk REQUIRED)
find_package(boost REQUIRED)
# Compile + Link
add_executable(FinalProject main.cpp)
target_link_libraries(FinalProject cpprestsdk::cpprest)
I've tried changing
target_link_libraries(FinalProject cpprestsdk::cpprest)
to
target_link_libraries(FinalProject boost ssl cpprestsdk::cpprest)
and many other variations to ensure that all the dependencies I'm using get loaded (OpenSSL, cpprestsdk, and Boost) however I can't seem to get any of these to work as I keep getting various errors like:
"library not found for "-lssl", "-lboost", "-lopenssl"
I installed openssl using homebrew but when I run homebrew link openssl I get: "Refusing to link macOS-provided software: openssl" and adding --force gives me the same error message.
I've created symbolic links in the paths mentioned here but to no avail.
Running the CMakeLists.txt with just the target_link_libraries(FinalProject cpprestsdk::cpprest) gives me:
Undefined symbols for architecture x86_64:
"boost::system::detail::system_category_ncx()", referenced from:
boost::system::system_category() in main.cpp.o
"boost::system::detail::generic_category_ncx()", referenced from:
boost::system::generic_category() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
But I can't figure out why since it seems Boost is loading fine (I get the message "Boost Version 1.68" when I build with target_link_libraries(FinalProject PRIVATE cpprestsdk::cpprest boost) but I also get the error ld: library not found for -lboost).
In my main.cpp (only file with code) I'm including:
#include <iostream>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
Anyone know what I'm missing? I'm guessing it's something simple in my CMakeLists.txt, but not sure. My only OpenSSL version is 1.0.2q and I'm on MacOS Mojave. Running openssl in my terminal does work, so it appears to be in my PATH. Really at a loss here as I'm not sure what's wrong at this point. Any help appreciated!
Specify that you want from boost. Here, you need "system":
find_package(Boost REQUIRED system)
Boost sets up variables to help after:
target_link_libraries(FinalProject ssl cpprestsdk::cpprest ${Boost_SYSTEM_LIBRARY})

CMake & dyld: Library not loaded: #rpath/libLTO.dylib

I am compiling my project using LLVM on Mac OS X with CLion and CMake.
My CMake configure is:
cmake_minimum_required(VERSION 3.6)
project(PPAP)
set(CMAKE_CXX_STANDARD 11)
# add_compile_options(-v)
include_directories(
/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/include/python3.6m
/usr/local/Cellar/llvm/3.9.1/include
)
set(SOURCE_FILES src/parser.cpp src/convert.cpp src/ast.cpp)
set(LIBRARIES
/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/libpython3.6.dylib
/usr/local/Cellar/llvm/3.9.1/lib/libLLVM.dylib
)
add_executable(PPAP ${SOURCE_FILES})
target_link_libraries(PPAP ${LIBRARIES})
Then I compile it successfully, but when I run it, I got:
dyld: Library not loaded: #rpath/libLTO.dylib
Referenced from: /usr/local/opt/llvm/lib/libLLVM.dylib
Reason: image not found
How to solve this problem?
Just linking libLLVM.dylib is not enough. Using llvm-config instead of adding libraries manually is a better way.
It's not ideal, but adding the library directories to the DYLD_LIBRARY_PATH environment variable in the CLion Run/Debug Configurations made the errors go away for me.
I'm still interested in a CMake-only solution without having to resort to DYLD_LIBRARY_PATH.

CMake, GLFW3 OSX linking error

I've struggled for a while now linking GLFW libraries in my simple C++ project in CLion. I've gone through the existing problems and their solutions but they do not seem to help in my case. My CMakeLists file is as follows:
cmake_minimum_required(VERSION 3.6)
project(TestGame)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -v")
find_library(carbon_lib Carbon)
find_library(cocoa_lib Cocoa)
find_library(iokit_lib IOKit)
find_library(corevideo_lib CoreVideo)
find_library(opengl_lib OpenGL)
find_library(corefoundation_lib CoreFoundation)
set(frameworks
${carbon_lib}
${cocoa_lib}
${iokit_lib}
${corevideo_lib}
${opengl_lib}
${corefoundation_lib}
)
file(GLOB sources
"include/*.hpp"
"src/*.cpp"
"src/application/*.cpp"
"src/controlling/*.cpp"
"src/helper/*.cpp"
"src/render/*.cpp"
"src/texturing/*.cpp"
"src/translations/*.cpp"
"test/*.cpp"
)
find_package(GLEW REQUIRED)
find_package(GLFW3 REQUIRED)
find_package(SDL2 REQUIRED)
find_package(GLM REQUIRED)
find_package(SOIL REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS} ${GLFW_INCLUDE_DIR} ${GLM_INCLUDE_DIR} ${SDL2_INCLUDE_DIR} ${SOIL_INCLUDE_DIRS} ${GLM_INCLUDE_DIRS})
add_executable(GameMain ${sources})
target_link_libraries(GameMain ${GLEW_LIBRARIES} ${GLFW_LIBRARIES} ${GLM_LIBRARIES} ${SDL2_LIBRARY} ${SOIL_LIBRARIES} ${frameworks})
I've got GLFW3 and GLFW2 (apparently v2 was required by Cocoa and I have no idea why) installed using brew (brew install glfw2 or brew install glfw3 - with --build-bottle --static it does not work because of internal cmake error), also tried the same with manually compiled GLFW from the latest sources.
It is to be noticed that I've recently started learning things about C++/CMake/CLion so I might forget about something important in here - do not hesitate to ask for more informations so I can add these.
It is also worth noticing that before Sierra OSX (currently running latest BETA) update my application just ran quite fine without linker errors. Linker errors are as follows:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
Application::Application(char const*, int, int) in Application.cpp.o
"_glfwMakeContextCurrent", referenced from:
Application::Application(char const*, int, int) in Application.cpp.o
"_glfwWindowHint", referenced from:
Application::Application(char const*, int, int) in Application.cpp.o
"_glfwWindowShouldClose", referenced from:
Application::run() in Application.cpp.o
and these relates to main entry files where a window is created. CLion does not make any suggestion about anything being omitted while including directories. I tried with/without all these frameworks listed about, found a solution where Cocoa/IOKit/CoreVideo/OpenGL are to be included in order to make GLFW3 working. Tries also including Carbon/CoreFoundation because some solutions were about these.
Literally nothing works.
http://www.glfw.org/docs/latest/build_guide.html#build_link_cmake_package
You need to add:
target_link_libraries(LearnGLSL ..any other libraries... glfw)

linking glbinding with cmake

I'm trying to use glbinding in my own project. I'm using cmake to build everything. The problem is linker cannot find this library. Probably I don't build library thus it cannot be linked, but I don't know how to achive that.
I've written linking code according to https://github.com/hpicgs/glbinding#linking-binaries.
Cmake:
set(SOURCE_FILES main.cpp)
add_executable(AKOpenGLEngine ${SOURCE_FILES})
set(CMAKE_PREFIX_PATH ${CMAKE_MODULE_PATH} glbinding )
find_package(glbinding REQUIRED)
include_directories(${GLBINDING_INCLUDES})
target_link_libraries(AKOpenGLEngine glbinding ${GLBINDING_LIBRARIES})
Error:
Linking CXX executable AKOpenGLEngine
ld: library not found for -lglbinding
main.cpp:
#include <glbinding/gl/gl.h>
int main(void) {
glbinding::Binding::initialize();
exit(EXIT_SUCCESS);
}
My current project structure:
Have you tried to remove the glbinding from target_link_libraries? ${GLBINDING_LIBRARIES} should be sufficient; it passes <your_specific_file_path_to_glbinding_library> to the linker. With -lglbinding the linker searches for a library within some default directories, your glbinding or build directory not included, thus throwing a library not found. To verify the content of ${GLBINDING_LIBRARIES} you can print it to cmake output, e.g., via message(STATUS ${GLBINDING_LIBRARIES}). However, i also suggest to integrate glbinding as external project as suggested by #janisz.
EDIT: sorry, didn't see the valid, but collapsed answer of #jet47

Fixing VTK 6.2 linking errors

Using VTK 6.2, there are multiple link errors when trying to make a project:
/usr/bin/ld: cannot find -lvtkWrappingTools
/usr/bin/ld: cannot find -lvtkGUISupportQt
/usr/bin/ld: cannot find -lvtkWrappingPythonCore
/usr/bin/ld: cannot find -lvtkFiltersPython
/usr/bin/ld: cannot find -lvtkGUISupportQtSQL
/usr/bin/ld: cannot find -lvtkRenderingQt
/usr/bin/ld: cannot find -lvtkglew
/usr/bin/ld: cannot find -lvtkGUISupportQtOpenGL
/usr/bin/ld: cannot find -lvtkLocalExample
/usr/bin/ld: cannot find -lvtkViewsQt
/usr/bin/ld: cannot find -lvtkoggtheora
/usr/bin/ld: cannot find -lvtkGUISupportQtWebkit
Problem:
Unresolved linker errors. This may be a path issue, since the libraries appear to be present in /usr/local/ but are not seen by ld at compile time...
Background information:
OS is Ubuntu 13.10
VTK is 6.2 from the Kitware github
CMake file follows guidelines supplied by the Kitware CMake for VTK 6+ docs
VTK was configured as an out-of-source CMake build (no problems) followed by make and make install so that all of the libraries are correctly placed in /usr/local/lib.
My project build now uses the following CMakeLists.txt:
# Add VTK, insist that it uses 6.2,
find_package(VTK 6.2 EXACT REQUIRED NO_MODULE)
include(${VTK_USE_FILE})
find_package(GLEW REQUIRED)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${VTK_DEFINITIONS})
include_directories(
${VTK_INCLUDE_DIRS}
${GLEW_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
####################
# Make my_target
####################
set(EXE my_target)
set(SOURCES
my_target.cpp
)
add_executable(${EXE} ${SOURCES})
target_link_libraries(${EXE} ${VTK_LIBRARIES} ${GLEW_LIBRARY} )
###################
Change the find_package parameters to specify just the modules that you want. For example:
find_package(VTK 6.2 EXACT REQUIRED COMPONENTS
vtkRenderingOpenGL vtkInteractionStyle NO_MODULE)
This 'fix' took some time to find. It looks like the default behaviour is for CMake to include all VTK modules ... some of which may not exist. If you specify components then the default behaviour is disabled. Unfortunately, it can be quite difficult to know what libraries to include, and so it would be nice to have the default "include everything" behaviour working!
My simple program didn't use Python or Qt, but ld still wants to resolve those basic libraries. This appears to be normal behaviour even when VTK is built without the Qt or Python modules.
This may be a bug in VTK 6.2. I'll raise it with Kitware, and revise the answer as/when new info is available...