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

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})

Related

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)

Error building Python extension using boost.python

I installed boost as well as boost-python, and boost-build using homebrew on my Mac with OS X 10.11.6. I am running Python 3.5.2. boost is set up correctly and works in C++ projects. Both, user-config.jam and the jamfile located in my python extension project directory are ok. I tried to compile a shared library from the following source
#include <iostream>
#include <boost/python.hpp>
using namespace std;
void say_hello() {
std::cout << "HELLO!!!!!";
}
BOOST_PYTHON_MODULE(hello) {
using namespace boost::python;
def ("say_hello", say_hello);
}
using the b2 interpreter. It issues the following command:
"g++" -dynamiclib -Wl,-single_module -install_name "hello.so" -L"/usr/local/lib/python3.5" -o "bin/darwin-4.2.1/release/hello.so" "bin/darwin-4.2.1/release/say_hello.o" -lpython3.5 -headerpad_max_install_names -Wl,-dead_strip -no_dead_strip_inits_and_terms
, which crash with
darwin.link.dll bin/darwin-4.2.1/release/hello.so
Undefined symbols for architecture x86_64:
"typeinfo for boost::python::objects::py_function_impl_base", referenced from:
[...long trace back ...]
"boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
_PyInit_hello in say_hello.o ld: symbol(s) not found for architecture x86_64
I am very aware of all the question concerning similar problems, but unfortunately none of them provides a working answer.
What do I have to do I order to get this simple code working as an Python extension module?
You should link it with boost python library as well( as boost.python is not header only). Here is how boost libraries are include in the build command(paths as I have on my machine):
-L/usr/lib/libpython2.7.dylib /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_python-mt.dylib /usr/lib/libpython2.7.dylib -Wl,-rpath,/usr/lib/libpython2.7.dylib
I assume you can do without libboost_system library. (Such output I get when run make VERBOSE=1 as I'm not running make explicitly.)
Regarding cmake, here is a simple CMakeLists.txt you can use to build a project with Boost.Python:
cmake_minimum_required(VERSION 2.8)
set(LIBRARY_NAME "ext") # ext for extension
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -ggdb")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(SOURCES
python_interface.cpp
main.cpp
)
set(CMAKE_MACOSX_RPATH ON)
# Get Boost
find_package(Boost COMPONENTS
system
python REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
# Get Python
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})
add_library(${LIBRARY_NAME} SHARED ${SOURCES})
target_link_libraries(${LIBRARY_NAME}
${Boost_LIBRARIES}
${PYTHON_LIBRARIES}
)

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

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})

Getting "ld: symbol(s) not found for architecture x86_64" when compiling cmake code

I'm trying to compile a ROS package which uses Boost. The code compiles just fine on Linux, but on OS X I'm getting the error
ld: symbol(s) not found for architecture x86_64
I installed Boost through brew and it seems that it is installed in 64bit (my system is also 64bit - OS X 10.9), as running
file libboost_atomic-mt.dylib
outputs
libboost_atomic-mt.dylib: Mach-O 64-bit dynamically linked shared library x86_64
In the CMakeFiles.txt, I've tried nearly everything in terms of compile and link flags, having tried -stdlib both as libc++ and as libstdc++, as well as -mmacosx-version-min as everything from 10.5 to 10.9. For instance, right now I have:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++ -mmacosx-version-min=10.9")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libstdc++ -mmacosx-version-min=10.9")
Furthermore, building with -m64 produces the same errors, and building with -m32 produces the same errors except that it says "... architecture i136".
The following paste bins contain my CMakefiles.txt and the compiling errors, respectively:
http://pastebin.com/0MD8T916 - CMakeFiles.txt
http://pastebin.com/v3vk9i2r - Errors
I'm running out of ideas for fixing this issue...
Thank you for your help!
You did not actually add/link the boost libraries inside your project.
set(BOOST_COMPONENTS
unit_test_framework
program_options
thread) # And other components you need
set(Boost_USE_STATIC_LIBS ON) # Easier to deploy elsewhere
set(BOOST_ROOT /usr/local/opt/boost) # Useful for boost from brew
set(BOOST_LIBRARYDIR /usr/local/opt/boost/lib64)
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(<your target> ${Boost_LIBRARIES})