How to connect llvm to a project - llvm

I configured cmake like this
cmake_minimum_required(VERSION 3.13.4)
project(SimpleProject)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
add_library(llvm STATIC ${PROJECT_SOURCE_DIR}/main.cpp)
target_include_directories(llvm PUBLIC ${PROJECT_SOURCE_DIR})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_compile_definitions(llvm PUBLIC MAIN)
# Now build our tools
add_executable(main main.cpp)
# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Link against LLVM libraries
target_link_libraries(main ${llvm_libs})
The whole problem is that the libraries are connected in the form llvm-12/llvm/.... , and not llvm/.../.../..
How to set up a project so that it includes libraries in the form #include "llvm/.../..."
#include "llvm/ADT/STLExtras.h" //it doesn't work, should be connected like this
#include "llvm-12/llvm/ADT/STLExtras.h" // it's work
#include "llvm-12/llvm/IRReader/IRReader.h" // it's work
#include <memory>

Related

How to determine what LLVM components used in source for cmake?

I'm new to cmake and LLVM. Here is a confusing question when I configure CMakeLists.txt for my own LLVM project.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.24)
project(llvm_in_practice)
set(CMAKE_CXX_STANDARD 14)
FILE(GLOB SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.cpp)
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
include_directories(include)
add_executable(main ${SOURCE_FILES})
llvm_map_components_to_libnames(llvm_libs core)
target_link_libraries(main ${llvm_libs})
I'm ok with most of all the configuration except for llvm_map_components_to_libnames(llvm_libs core). In my source, I use #include <llvm/IR/Value.h> etc. to include LLVM into my project. However, in cmake, I need to know what component the header file belongs to for making cmake right.
I'v tried llvm_map_components_to_libnames(llvm_libs ir) which obviously not work. I also tried llvm-config --components to see all components but I do not know how to determine which one I should use.
How can I checkout which component the LLVM header file belongs to?

Cmake CPM link with GameNetworkingSockets

I am trying to link Valves GameNetworkingSockets with my c++ cmake project, and am having errors (first failed to compile due to missing defines when I was able to include the headers, later errors were due to unrecognized targets...). I am using the CPM module for package installing. Below is my cmake:
# Basic settings
cmake_minimum_required (VERSION 3.14)
project (Valhalla)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(get_cpm)
if (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
endif()
############
# LIBRARIES
############
CPMAddPackage(
NAME gamenetworkingsockets
GITHUB_REPOSITORY ValveSoftware/GameNetworkingSockets
#VERSION 3.11.3
GIT_TAG v1.4.1
DOWNLOAD_ONLY TRUE
OPTIONS
CONFIG REQUIRED
"USE_STEAMWEBRTC ON"
"Protobuf_USE_STATIC_LIBS ON"
)
##add_library(lua STATIC ${Lua_sources})
#add_library(steamnet STATIC ${gamenetworkingsockets_sources})
add_library(gamenetworkingsockets INTERFACE)
target_compile_definitions(gamenetworkingsockets INTERFACE STEAMNETWORKINGSOCKETS_STATIC_LINK)
target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets PUBLIC ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets PUBLIC ${gamenetworkingsockets_BINARY_DIR})
#target_compile_definitions(gamenetworkingsockets PUBLIC STEAMNETWORKINGSOCKETS_STATIC_LINK)
#target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_BINARY_DIR}/include)
#target_include_directories(gamenetworkingsockets PRIVATE ${gamenetworkingsockets_INCLUDE_DIR})
#
# Linking
#
add_executable(Valhalla
"src/Main.cpp"
)
target_include_directories(Valhalla PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(Valhalla PRIVATE cxx_std_20)
target_link_libraries(Valhalla
#gamenetworkingsockets
#GameNetworkingSockets::shared
#steamnet::steamnet_s
#GameNetworkingSockets::GameNetworkingSockets_s
#GameNetworkingSockets::static
#GameNetworkingSockets::GameNetworkingSockets_s
)
set_target_properties(Valhalla PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/run/
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
if (MSVC)
target_compile_options(Valhalla PRIVATE /bigobj "/diagnostics:caret")
endif()
if(MSVC AND CMAKE_BUILD_TYPE MATCHES Release)
set_target_properties(Valhalla PROPERTIES WIN32_EXECUTABLE ON)
endif()
The Cmake above has been stripped to a mostly minimal portion for reproducibility. As you can see, there are many commented-out commands as I have tried to brute force ways of making this work to no avail. I found this Linking with GameNetworkingSockets library on Windows gives "unresolved external" error which doesn't help.
#include <steam/steamnetworkingsockets.h>
int main(int argc, char **argv) {
SteamNetworkingIdentity identity;
identity.ParseString("str:peer-server");
return 0;
}
I have no clue how to interface vcpkg with Cmake from MSVC, and I really do not know how to fix this. Any clues as to how I can get everything to link and eventually compile correctly?
So I found out how to use vcpkg.
Install vcpkg
git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
Update it with
git pull
Make libraries installed with vcpkg recognizable by msvc
.\vcpkg\vcpkg integrate install
Install GameNetworkingSockets and required Protobuf with
.\vcpkg\vcpkg install GameNetworkingSockets --triplet=x64-windows
.\vcpkg\vcpkg install protobuf --triplet=x64-windows
cmakelists.txt in msvc
# Project
cmake_minimum_required (VERSION 3.14)
project (MyProject)
# Libraries
find_package(protobuf CONFIG REQUIRED)
find_package(GameNetworkingSockets CONFIG REQUIRED)
# Linking
add_executable(${PROJECT_NAME}
"src/Main.cpp"
)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
target_link_libraries(${PROJECT_NAME}
PRIVATE GameNetworkingSockets::static GameNetworkingSockets::GameNetworkingSockets
PRIVATE protobuf::libprotobuf
)
Try this simple script
// main.cpp
#include <steam/steamnetworkingsockets.h>
int main(void) {
SteamNetworkingIdentity identity;
identity.ParseString("str:peer-server");
return 0;
}
The above should compile fine if everything worked

How to integrate Third party libraries into a Portable Custom Library

I have been trying to figure out how to link in CMake and it is making me go crazy. So far I have successfully been able to run my library as an executable(add_executable), but when changing it to a library(add_library) and trying to include it in another project it falls short. The issue is including the third party libraries in my library. The third party libraries are not found and therefore are failed to be included in executable project which results missing libraries. I have tried doing it by statically compiling, but I got lost and fell back to shared libraries.
Basically I am trying to figure out how to compile a library so that its dependencies are portable with the library.
Example Header
#ifndef APP_H
#define APP_H
#include <iostream>
#include <vector>
#include <cstdint>
#include <SDL.h>
#include <SDL_ttf.h>
#include <Box2D/Box2D.h>
#include "Class.h"
#include "Vec2.h"
#include "Vec4.h"
using namespace std;
class App{
/*contents*/
};
#endif
Here is my main CMakeList.txt
add_subdirectory(extern)
cmake_minimum_required(VERSION 3.7)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(PROJECT_NAME Project)
project(${PROJECT_NAME} VERSION 0.1.0)
add_subdirectory(src)
Here is my source CMakeList.txt
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
file(GLOB EXTERN_SOURCES "${CMAKE_SOURCE_DIR}/extern/stb/include/*.h" "${CMAKE_SOURCE_DIR}/extern/stb/src/*.cpp" )
include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include/Chassis2D" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include/" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2_ttf" PUBLIC "${CMAKE_SOURCE_DIR}/extern/stb/include/")
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADER_FILES} ${EXTERN_SOURCES})
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2 PUBLIC SDL2_ttf PUBLIC box2d)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
Thanks in advance!

CPPREST SDK on CLion using CMake on Mac

I am attempting to use the CPPREST SDK in CLion on a Mac using CMake. I'm almost there, but I cannot seem to resolve the following linker error:
Undefined symbols for architecture x86_64:
"_ERR_remove_thread_state", referenced from:
boost::asio::ssl::detail::openssl_init_base::do_init::~do_init() in PongRemote.cpp.o
I have specified -lssl and -lcrypto, but still get this "thread state" error. Based on some research, it looks like it is coming from OpenSSL. I used Homebrew to install CPPREST.
I have tested this with literally just a main and the basic includes:
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
with a CMake of:
project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 14)
# BOOST PACKAGE
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
random
regex
serialization
system
thread
)
include_directories(${Boost_INCLUDE_DIRS})
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
# Microsoft RESTful API Package (Casablanca)
set(CPPREST_LIBRARIES "/usr/local/opt/openssl/lib")
include_directories("/usr/local/opt/openssl/include")
# Compile and link
# Build the core library and executable
include_directories(${CMAKE_SOURCE_DIR})
set(SOURCE_FILES
main.cpp
)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${CPPREST_LIBRARIES}
-lssl
-lcrypto
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})
After taking some time, the linking problem was due to the CMake find commands not working properly. I manually pointed directly to the libraries for both OpenSSL and RESTCPP and the problem was fixed.
Project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
# BOOST PACKAGE
set(Boost_USE_MULTITHREADED ON) # Default ON
set(Boost_USE_STATIC_LIBS ON) # Default OFF
set(Boost_USE_DEBUG_RUNTIME OFF) # Default ON
set(Boost_USE_DEBUG_PYTHON OFF) # Default OFF
set(Boost_USE_STLPORT OFF) # Default OFF
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
program_options
random
regex
system
serialization
thread
)
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE("Boost_INCLUDE_DIRS:\t" ${Boost_INCLUDE_DIRS})
# Open SSL Package
set(OpenSSL_INCLUDE /usr/local/opt/openssl/include)
set(OpenSSL_LIBRARIES
/usr/local/opt/openssl/lib/libcrypto.dylib
/usr/local/opt/openssl/lib/libssl.dylib)
include_directories(${OpenSSL_INCLUDE})
# Microsoft RESTful API Package (Casablanca)
set(CppREST_INCLUDE /usr/local/opt/cpprestsdk/include)
set(CppREST_LIBRARIES /usr/local/opt/cpprestsdk/lib/libcpprest.dylib)
include_directories(${CppREST_INCLUDE})
# Compile and link
# Build the core library and executable
set(SOURCE_FILES main.cpp)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${OpenSSL_LIBRARIES}
${CppREST_LIBRARIES}
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})

Not sure how to add opencv library to CMakeLists.txt

I hope someone can help me.
I have a simple CMakeLists.txt in order to build my project on Ubuntu. I'm using CMake 2.8.1 and at the moment this is the code:
cmake_minimum_required(VERSION 2.4.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)
rosbuild_init()
find_package(OpenCV 2)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()
#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
rosbuild_add_executable (RosPub src/paste.cpp)
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
I need to add opencv libraries on my project. I have added them but i can't still get my code to work. its keeps posting me this error:
‘struct MyOpenNIExample::ImgContext’ has no member named ‘image’
there is a few of them.
after i added find_package(OpenCV REQUIRED to the CMakeLists.txt,
i get this error
Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
directory containing a CMake configuration file for OpenCV. The file will
have one of the following names:
OpenCVConfig.cmake
opencv-config.cmake
what shld i do? I am using Apple and using Ubuntu 10.04.
Since i need
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
i added
find_package(OpenCV 2),
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace) and
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
the include files above are included in the vision_opencv in ROS. so to include it, add the opencv dependency in the manifest file.
That would help.
Get FindOpenCV.cmake from above link and put it anywhere on your computer.
After cmake_minimum_required add line set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [path to folder where you put FindOpenCV.cmake])
Add find_package(OpenCV) to your CMakeLists.txt
On this step you can check for OpenCV_FOUND and other OpenCV variables in your CMakeLists.txt