I am working on a c++ project using cmake that uses hiredis. The CMake and compilation process do not give any errors. However, when I try to execute my project (from the terminal or from the IDE I'm using [CLion], I get the following error:
dyld: Library not loaded: libhiredis.0.13.dylib
Referenced from: /Users/connorriley/CLionProjects/DispatchingOptimization/bin/dispatch
Reason: image not found
I'm not sure why my project is looking for libhiredis.0.13.dylib because the only hiredis library file I have is libhiredis.dylib.
My project file structure is the following:
.
+-- bin
| +-- dispatch (my executable)
+-- lib
| +-- hiredis
| | +-- libhiredis.dylib
| +-- otherlibs
+-- src
| +-- source code/subfolders with source code
additional info:
compiler: clang
os: macOS 10.12.3
cmake version 3.7.2
Looks like your DYLD_LIBRARY_PATH is not set correctly. You can get more information by setting DYLD_PRINT_LIBRARIES and/or some other environment variables mentioned here
But probably you just need to add your hiredis directory to CMAKE_LIBRARY_PATH like this:
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${PROJECT_SOURCE_DIR}/lib/hiredis)
I fixed my issue, it was that I went into my hiredis directory and typed:
make
but didn't follow that with
make install
Therefore, the file that my code was looking for was not in my /usr/local/lib
Related
I'd like to use a few classes from the LLVM project in my own Objective-C app. Specifically, I want to use the classes declared in BitstreamReader.h and BitstreamWriter.h. Unfortunately, I don't have much experience linking C++ libraries, so I don't really know where to begin. I started by installing llvm through Homebrew with brew install llvm#14. Then, in my Xcode project, I tried linking the libraries in /opt/homebrew/opt/llvm#14/lib, and adding /opt/homebrew/opt/llvm#14/include/** to my HEADER_SEARCH_PATHS.
Now I'm completely stuck. I have a bunch of build errors in Xcode like:
"Reference to unresolved using declaration"
"No member named 'ldiv' in the global namespace"
"Use of undeclared identifier 'wcspbrk'"
...
Any help at all would be greatly appreciated. Thanks!
In C++ world there are quite a few ways to incorporate a dependency into your project, I'll outline some of them, but will try to describe in details one which I find the most suitable for your scenario:
1. Installing libraries system/user-wide
This is the most classic approach, when you have the libraries pre-compiler and pre-installed for your specific platform so they are available under default library search-path. It's similar to how system frameworks in iOS are used - you only add linker command to the project, while the frameworks (libraries) exist independently from it (or don't exist, which causes linker error). The problem with this approach, is that you cannot really use the libs for restricted systems, where "default" libraries are unchangeable (iOS, tvOS, iPadOS)
2. Embedding static libraries
Another approach is to precompile all libraries you need into archives for all platforms the libraries are supposed to be used and then just embed one of the library version which is required into your final app binary. This approach is somewhat cumbersome and unportable because it will require you to compile the lib and re-embed it manually each time you need some parts of library changed or new platform supported.
3. Embedding an xcframework
This approach is very similar to the previous one with a few benefits that you can wrap binaries for all platforms under one package and even release it in SPM.
4. Using CMake build system
Many projects (and LLVM is not an exception) in C++ are made with use of so-called CMake tool. It's widely adopted multi-platform build system and one of the benefits you can employ is that you can easily include any other project as part of your own. At the same time CMake is not widespread at all in the world of mobile development, so you may have hard time finding relevant information for these platforms.
5. Using Xcode workspaces
This is the solution I'd like to describe in more details. In a nutshell this approach consists of 6 steps:
Download LLVM project repo;
Generate Xcode project for required LLVM libraries with use of CMake;
Make an Xcode workspace and add the newly generated project to it;
Add your own project to the same workspace;
Add required dependencies from LLVM project to your own;
Adjust project settings to make it compatible with the dependencies.
The benefits of this approach is that it gives the most consistent experience (after generating project, all parts are configurable from Xcode), freedom of managing the source code (the LLVM project files can be altered however you want), and luxury of built-in dependency graph (all libraries required to build a target are given under Xcode settings).
Downloading LLVM project
You can clone the project from here. Be advised that this has to be in the same folder your future workspace will be in, so you better prepare it in advance:
% mkdir MyWorkspace && cd MyWorkspace
% git clone git#github.com:llvm/llvm-project.git
Generating LLVM Xcode Project
First, ensure you have CMake installed (it doesn't come out of the box with macOS). You can use brew or just download the app from the official site. After that create a new folder for the future Xcode project next to llvm-project repo folder and run cmake on the LLVM toolkit project:
% mkdir LLVM && cd LLVM
% cmake -GXCode ../llvm-project/llvm
Creating Xcode workspace
Nothing fancy here, just open Xcode and navigate to File/New/Workspace menu (Ctrl+Cmd+N shortcut). Ensure that your workspace is created in the MyWorkspace folder. You can just do it from Xcode:
Then, add the LLVM project created on the previous step. Open File/Add Files to "MyWorkspace"... menu (Option+Cmd+A) and select the Xcode project file:
If Xcode suggests to auto-create schemes I recommend accepting this suggestion so you don't have to deal with it yourself later on.
Adding your own project
This step mimics the previous one with exception that you add your own project to the workspace. I didn't have an existing project for that, so I just created a new one (in my case macOS command-line app) in the workspace directory. If you do the same, ensure that the project is added to "MyWorkspace" and the folder is correct:
Eventually your workspace "Project Navigator" should look something like this:
And here is how the directory tree looks like in a nutshell:
% tree -L 2
.
|-- LLVM
| |-- $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
| |-- CMakeCache.txt
| |-- CMakeFiles
| |-- CMakeScripts
| |-- CPackConfig.cmake
| |-- CPackSourceConfig.cmake
| |-- Debug
| |-- LLVM.xcodeproj
| |-- MinSizeRel
| |-- RelWithDebInfo
| |-- Release
| |-- benchmarks
| |-- build
| |-- cmake
| |-- cmake_install.cmake
| |-- docs
| |-- examples
| |-- include
| |-- lib
| |-- llvm.spec
| |-- projects
| |-- runtimes
| |-- test
| |-- third-party
| |-- tools
| |-- unittests
| `-- utils
|-- MyProject
| |-- MyProject
| `-- MyProject.xcodeproj
|-- MyWorkspace.xcworkspace
| |-- contents.xcworkspacedata
| |-- xcshareddata
| `-- xcuserdata
`-- llvm-project
|-- CONTRIBUTING.md
|-- LICENSE.TXT
|-- README.md
|-- SECURITY.md
|-- bolt
|-- clang
|-- clang-tools-extra
|-- cmake
|-- compiler-rt
|-- cross-project-tests
|-- flang
|-- libc
|-- libclc
|-- libcxx
|-- libcxxabi
|-- libunwind
|-- lld
|-- lldb
|-- llvm
|-- llvm-libgcc
|-- mlir
|-- openmp
|-- polly
|-- pstl
|-- runtimes
|-- third-party
`-- utils
Adding dependencies to your project
From this point forward you'll only need Xcode to finish the job. First, let's link the libraries you need to the project and start from the Bitstream archive. Open your project's target General tab settings and under Framework and Libraries click the + sign which should take you to the screen with all local dependencies currently available. It's quite a long list of tools from the LLVM project + native Apple libs, so you may want to filter it as needed to find the required position:
Now the tricky part. You might not actually be required to do that, but if the LLVMBitstreamReader target itself relies on symbols of other libraries (and exposes such a dependency with explicit use of the libraries symbols) the project linking will fail, so to be safe go to the LLVMBitstreamReader build settings and check what it depends on under Target Dependencies section:
Now add these dependencies to your project as well. Eventually your Frameworks and Libraries section should look like this:
Adjusting project settings
CMake generated Xcode projects come with peculiarities. In our case the build folders for the LLVM project targets are located under the project directory with the name matching selected configuration (Debug/Release/MinSizeRel/RelWithDebInfo). In order for your own project's targets to find the libraries built with the given configuration you either have to adjust the LLVM project/targets settings, which I don't recommend because it's a lot of manual work, or just add custom library search path to your project. For the latter go the Build Settings of the target where you added the dependencies at the previous step, find Library Search Path item and add $(SRCROOT)/../LLVM/$(CONFIGURATION)/lib as a new item:
Another tricky part is that dependent targets won't make dependencies to build if the dependencies' targets have incompatible build settings. Incompatible is a vague term here, but commonly it means matching the Architectures section. Luckily in our case it merely means making Release configuration to build active architecture only (the Debug shouldn't require any changes at all):
Last, but not least, the headers of the library. After generating the LLVM Xcode project, the headers it offers don't (completely) come with the project and are actually located in the repository folder. You may just copy the headers right into your own project, but I suggest adding the repo directory in the System Headers Search paths to stay consistent with the LLVM Xcode Project settings (you can yourself check where the headers search paths are under the the LLVMBitstreamReader Build Settings). Similarly to lib search path, I suggest use help of settings variables and add this path flexibly:
$(SRCROOT)/../llvm-project/llvm/include
$(SRCROOT)/../LLVM/include
Finale
At this point you should be good to use the LLVMBitstreamReader library and classes defined there. I renamed my main.m to main.mm so clang knows i'm going to use C++ in my code, and here is my sample code:
//
// main.mm
// MyProject
//
// Created by Aleksandr Medvedev on 14.12.2022.
//
#import <Foundation/Foundation.h>
#import <llvm/Bitstream/BitstreamReader.h>
#import <iostream>
int main(int argc, const char * argv[]) {
llvm::BitstreamBlockInfo::BlockInfo info;
info.Name = "Hello, LLVM!";
std::cout << info.Name << std::endl;
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
}
return 0;
}
If everything is done right, it should compile without any errors now.
P.S. I many times focused on specific directory hierarchy for a reason - when an XCode project is generated through CMake it often uses absolute paths, not relative and if you move your project to another directory, you will have to either adjust the paths in the build settings of required targets/projects or repeat the step with generating the LLVM Xcode project again.
Consider this project structure:
Complete example of the project: another-ghasem/CMakePluginProblem
+
|
+-- plugin/
+- plugin_1/
+- plugin_2/
+- plugin_3/
+-- CMakeLists.txt
+-- main.cpp
Those plugins are Shared Libraries created with CMake and will be linked to the root project. But the plugins dependencies are the problem (all the plugins have a Target Alias with BUILD_INTERFACE include path: plugin_2/CMakeLists.txt).
How could we manage the non-generated but linked plugins?
Consider the plugin_1 linked to plugin_2 (plugin_1/CMakeLists.txt#22):
target_link_libraries(${PROJECT_NAME} PRIVATE Project::plugin_2)
Here CMake could somehow handle the build order, but if we remove the plugin_2 CMake will raise an error about it:
Target "plugin_1" links to target "Project::plugin_2" but the target was
not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?
By the way, we couldn't use an if-statement to check "if we have the needed plugin, then linked and define a macro to notice that we have the plugin". How could we handle these dependencies?
- CMake Version: 3.16
- OS: Fedora 34, Ubuntu 18.04
I'm pretty new to C++. I've found a hash library I want to use: https://github.com/aappleby/smhasher
What is the right way of including that library in my own code?
Is it common to clone other repos directly into a lib folder and build the code within?
My folder structure is currently:
|-include
| |- myfile.h
|-src
| |- myfile.cpp
| |- main.cpp
|-lib
| |-smhasher
| |- MurmurHash3.h
| |- MurmurHash3.cpp
| ... etc
|- Makefile
and my makefile currently builds MurmurHash3.cpp before building myfile.cpp before building main.cpp
It's a bit weird to me because this setup needs to include the smhasher library in my source code when pushing to github for version control. I can gitignore the lib folder but then ppl using my code need to be aware they are required to clone smhasher into the lib folder.
Alternatively I'm wondering if I should go into the smhasher source code and build a static library and include that in a folder here.
Basically I'm wondering what is the most common way to handle external dependencies in c++? Are there benefits of doing it one way or the other?
The most common way would probably be to clone the repo, put .hinto /include/ and .cpp into /src/. Then adjust makefile accordingly.
I am trying to deploy a C++ application compiled with gcc on Linux by putting the required .so files into the executable directory. I added the linker flag -Wl,-rpath=$ORIGIN so that the program may look for the linked libraries in the directory where it's located. This works so far as that all libraries that are directly linked with my executable are found (checked via ldd).
However, when I try to launch the application I get the following error:
This application failed to start because it could not find or load the Qt platform plugin "xcb".
Available platform plugins are: linuxfb, minimal, offscreen, xcb.
Reinstalling the application may fix this problem.
The platform plugins are located in the folder ./platforms (relative to the executable path). Those some other shared object files which are apparently loaded by Qt, one of them being libqxcb.so. Now, the problem is that this file again depends on libQt5Gui.so, libQt5Core.so etc. These are located in my application path, but I suspect that the libqxcb.so is somehow not able to find them there, thus it fails. Is there a possibility how I could fix this?
If I use the following script to run the application, it works (note: Ct is the name of the executable):
#!/bin/sh
DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR
LD_LIBRARY_PATH=LD_LIBRARY_PATH:. ./Ct
But I would like to achieve this without having to use a script to run the application.
The qt deployment document is not particularly helpful with this.
The key to solving this issue is when you look at ldd output of libqxcb.so it goes in the lib folder.
libQt5Core.so.5 => <*>/plugins/platforms/./../../lib/libQt5Core.so.5 (0x00007f5f8374a000)
Therefore the directory structure should be as following:
app
|-- lib
| |-- libQt5Core.so.5
| |-- libQt5Gui.so.5
| |-- libQt5DBus.so.5
| |-- libQt5XcbQpa.so.5
| |-- libicui18n.so.56
| |-- libicuuc.so.56
| `-- libicudata.so.56
|-- qt.conf
|-- app_exec
`-- plugins
`-- platforms
`-- libqxcb.so
In project.pro set your application rpath for lib folder:
unix:!mac{
QMAKE_LFLAGS += "-Wl,-rpath,\'\$$ORIGIN/lib\'"
}
Finally you need to set up qt.conf for your app to be able to find plugins (by default looks from the platforms folder):
[Paths]
Prefix=./
Libraries=lib
Plugins=plugins
I started playing around with CMake to create a project with Qt and test it with Google Test. At the moment, I succesfully found a way to compile and link all the required libraries. However, I couldn't find a way to link sources to test files with following project structure:
root
|
+-- CMakeLists.txt
+-- src
| |
| +-- CMakeLists.txt
| +-- MyClass.h
| +-- MyClass.cpp
|
+-- test
| |
| +-- CMakeLists.txt
| +-- MyClassTest.cpp
|
+-- lib
|
+-- gtest-1.6.0
|
+-- CMakeLists.txt
Root CMakeLists.txt contains add_subdirectory for gtest, src and test folders. I have succesfully compiled and run "Hello world" app and simple EXPECT_TRUE(true) test in order to check that each part compiles correctly. Unfortunately, I couldn't find a way to include my source file to tests. Is it possible with the following project structure?
PS I know that it is possible to compile my sources as a library and link it to tests, but I dislike that approach, since it is more appropriate for integration testing, rather then unit testing...
EDIT: Added class names to the tree
You can add a global variable at the level of your root CMakeLists.txt:
set(ALL_SRCS CACHE INTERNAL "mydescription" FORCE)
In the first add_subdirectory(src), you can do:
set(ALL_SRCS ${ALL_SRCS} blabla.cpp CACHE INTERNAL "description")
And in the add_subdirectory(test), you continue with:
set(ALL_SRCS ${ALL_SRCS} bla_test.cpp CACHE INTERNAL "description")
You can then do, add_executable, or library or whatever, with all your sources files.
EDIT: add trick for global variables in CMake.
In the root CMakeLists.txt you can add a include_directories(src) This will then also be used by the tests. Another thing you can do is in the test CMakeLists.txt add a include_directories(${<projectName>_SOURCE_DIR}) where projectName is the name specified using project(myproj) in the src/ CMakeLists.txt (if you specified a project in there of course. Also check the docs about project)