How to use the LLVM IR API - c++

I have just finished building LLVM with ninja, clang-cl and cmake, and I was wondering how do I can get this working so I can include LLVM headers and libraries in my C++ visual studio project. I want to do something similar to what Kaleidoscope does, the tutorial by LLVM.
I am just wondering how I can compile my project using llvm headers and libraries. Should I copy the llvm build into the project so the headers work? Should I shange the project settings to include libs/dlls that are required? How should I do this?

Figured it out!
First I rebuilt with visual studio (release x64). Then I used cmake to generate .sln filed for project (instead of trying to use llvm-config). The important part was to set LLVM_DIR to the right location, for me this was llvm-project\llvm\cmake\modules.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)
set(LLVM_DIR C:\\...\\llvm-project\\llvm\\cmake\\modules)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_executable(test test.cpp)
llvm_map_components_to_libnames(llvm_libs support core irreader)
target_link_libraries(test ${llvm_libs})

Related

Very slow intellisense for CMake-configured Vulkan-project

I have a Vulkan project configured with the following CMakeLists.txt on Windows:
cmake_minimum_required(VERSION 3.20)
project(Custom_Vulkan)
set(MINGW_INCLUDE "C:\\msys64\\mingw64\\include")
find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED HINTS "C:\\msys64\\mingw64\\lib\\cmake")
add_subdirectory(shaders)
add_executable(AppTest main.cpp)
target_link_libraries(AppTest PUBLIC Vulkan::Vulkan glfw)
include_directories(${MINGW_INCLUDE})
Extensions for CMake (CMake & CMake Tools), and C++ (C/C++, C/C++ Extension Pack, C/C++ Themes) are installed, and I have configured and built my executable using the CMake Tools-extension.
Intellisense can at times be unreasonably slow and freezes completely, resulting in 0 autocompletion suggestions for both constants from built-in libraries like M_PI from cmath, but also for the Vulkan/GLFW-APIs which have been located by CMake.
Where should I search in order to locate the dependency that stalls Intellisense?
Is it possible to monitor/profile the Intellisense-search in order to find the issue?
The CMake-tools project is fully able to detect the dependencies, and compiles fine.
The Intellisense-freezes were in this case caused by dependency-issues between a FetchContent repository dependency in CMake and the C# vscode extension pack which caused an OmniSharp server-handle to remain open.
This was a standard FetchContent-pull:
FetchContent_Declare(
imgui_repo
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
)
FetchContent_MakeAvailable(imgui_repo)
Right-clicking the extension followed by disable(workspace) solved the issue.

Building TBB using Visual Studio 2015 x64 and CMake

i would like to build TBB to use it in another CMake project. I tried to build TBB from the Github source using the makefile (upgraded with VisualStudio 2015). This failed due to a mysterious error:
LINK : fatal error LNK1181: cannot open input file 'opencv_core300.lib'
Where does this error could originate from?
My second try was is to build TBB using another repository that allows a build using CMake. This build produces an tbb.lib, tbb.dll, etc. file.
Now I am stuck how to incorporate is in my other cmake files. There is no TBBConfig.cmake or similar.
My CMakeLists.txt for my project looks like this:
cmake_minimum_required(VERSION 3.10)
project(IntrafraktionelleRegistrierung)
find_package(ITK REQUIRED
COMPONENTS
ITKRegistrationCommon
ITKRegistrationMethodsv4
)
include(${ITK_USE_FILE})
set(SRC
${CMAKE_PROJECT_NAME}.cxx
)
if (DEFINED ENV{TBBROOT})
message(STATUS "TBBROOT: $ENV{TBBROOT}")
else()
message(STATUS "TBBROOT not defined!")
endif()
find_package(TBB REQUIRED)
add_executable(${CMAKE_PROJECT_NAME} ${SRC})
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
tbb
)'
TBBROOT is the build directory of tbb. The FindTBB.cmake I have available is borrowed from here and copied to the modules directory of cmake.
The latest version of the binaries of TBB have a CMake folder with TBBConfig.cmake inside. I used this to link the TBB to my project but somehow I ended up by an error stating: "tbb-NOTFOUND.obj cannot be found". (This way is still under investigation.
Has someone used this repository to configure and build a cmake project?
About CMake related questions
Basically you have two options:
Integration of pre-built TBB binaries into your project
You can use the binaries of TBB (just as you did) according to the following example. After invocation of find_package(TBB REQUIRED) you will get TBB targets in a format TBB::<component> (e.g. TBB::tbb, TBB::tbbmalloc, etc). Also TBB_IMPORTED_TARGETS variable will contain all imported TBB targets.
So, you need to slightly modify your target_link_libraries:
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
${TBB_IMPORTED_TARGETS}
)
or
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
TBB::tbb
)
Also you can update your find_package if you need only TBB::tbb component in your project: find_package(TBB REQUIRED tbb)
Integration of TBB source code into your project
You can use tbb_build (it is a CMake wrapper using GNU Make on TBB Makefiles), but to run it on Windows under Visual Studio you will need to have GNU Make in your environment.
If you'd like to integrate this TBB (with CMake support) you can use add_subdirectory(<YOUR-TBB-ROOT>) (replace <YOUR-TBB-ROOT> with actual location of TBB) instead of find_package(TBB REQUIRED).

Unable to link LLVM compiler tutorial program without building LLVM with RTTI

So I'm trying to follow along with the LLVM compiler tutorial and I'm trying to build the toy example using CMake instead of llvm-config. Because of an issue with Ubuntu's packaging system, I've decided to build LLVM from source and link to that. So I followed the instructions here to build LLVM 3.7.1 from source. I copy-pasted the Chapter 3 source code from LLVM's examples/Kaleidoscope directory (since the tutorial linked above recommends doing so for the specific version of LLVM you use) and attempted to build it with the following CMakeLists.txt, adapted heavily from this:
cmake_minimum_required(VERSION 3.5.1)
project(llvm-test-project)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "LLVM built with RTTI? ${LLVM_ENABLE_RTTI}")
add_executable(toy toy.cpp)
set_property(TARGET toy PROPERTY CXX_STANDARD 11)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
llvm_map_components_to_libnames(llvm_libs core support)
target_link_libraries(toy ${llvm_libs})
message(STATUS "LLVM linked to: ${llvm_libs}")
However, when I try to configure and build it, I get the following linker error:
undefinted reference to `typeinfo for llvm:CmpInst`
After attempting to link to other libraries other than core and support, I decided to try re-building LLVM with RTTI enabled, and sure enough, it compiles and links successfully when I add -DLLVM_ENABLE_RTTI=ON to the LLVM cmake invocation.
Is it possible to build LLVM without RTTI and still successfully link it to the Kaleidoscope examples? It seems strange that RTTI would be needed without any mention of such from either the tutorial or the LLVM CMake documentation.
Figured it out! Turns out that I my project was being built by default with RTTI, so it only linked properly when LLVM was also being built with RTTI. Adding the -fno-rtti compile flag fixed the problem.

CLion unable to recognize OpenCV libraries (no IntelliSense)

I am trying to solve this thing for quite some time. After a few hours I just decided to post a question because I am out of ideas. I imported a project into CLion with the CMakeLists.txt already inside. I am working on ubuntu and have the newest cmake version (3.5.something, OpenCV is 2.4.x). I use clion just for the IntelliSense, I wont be using it for the compilation anyway (I use terminal for that).
By opening the project I get following warning messages (which I somehow succeeded to reduce to warnings). This happens for every add_library line in OpenCVModules.cmake (I post just one of them):
CMake Warning (dev) at /usr/local/share/OpenCV/OpenCVModules.cmake:53 (add_library):
ADD_LIBRARY called with SHARED option but the target platform does not
support dynamic linking. Building a STATIC library instead. This may lead
to problems.
Has anyone got ideas?
#CMakeLists.txt
# OpenCV:
find_package(OpenCV REQUIRED)
include_directories(${OPENCV_INCLUDE_DIR})
IF(OpenCV_FOUND)
MESSAGE(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
ELSE(OpenCV_FOUND)
MESSAGE(STATUS "OpenCV_LIBS not found!")
ENDIF(OpenCV_FOUND)
project(cvtask1a)
file(GLOB SOURCES ${SOURCE_WILDCARDS})
include_directories(${CMAKE_SOURCE_DIR}/cgcvcommon)
add_executable(cvtask1a ${SOURCES})
target_link_libraries(cvtask1a ${OpenCV_LIBS})

Reference boost from CMake in CLion

CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I'm trying to reference boost in CMakeLists.txt
set(BOOST_ROOT "O:/Project/lib/windows/boost_1_59_0")
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
If there is no libraries needed, so I use
find_package(Boost 1.59.0)
boost is found and all works well.
But when I'm trying to reference libraries "Boost_FOUND" is not set
Boost libraries is built and there are following files in the O:/Project/lib/windows/boost_1_59_0/stage/lib folder
boost_filesystem-vc120-mt-1_59.dll
boost_filesystem-vc120-mt-1_59.lib
boost_filesystem-vc120-mt-gd-1_59.dll
boost_filesystem-vc120-mt-gd-1_59.lib
boost_system-vc120-mt-1_59.dll
boost_system-vc120-mt-1_59.lib
boost_system-vc120-mt-gd-1_59.dll
boost_system-vc120-mt-gd-1_59.lib
libboost_filesystem-vc120-mt-1_59.lib
libboost_filesystem-vc120-mt-gd-1_59.lib
libboost_filesystem-vc120-mt-s-1_59.lib
libboost_filesystem-vc120-mt-sgd-1_59.lib
libboost_filesystem-vc120-s-1_59.lib
libboost_filesystem-vc120-sgd-1_59.lib
libboost_system-vc120-mt-1_59.lib
libboost_system-vc120-mt-gd-1_59.lib
libboost_system-vc120-mt-s-1_59.lib
libboost_system-vc120-mt-sgd-1_59.lib
libboost_system-vc120-s-1_59.lib
libboost_system-vc120-sgd-1_59.lib
What I missed?
Probably because you want to build it your project with MinGW, but your libraries are compiled for Visual studio (you can see it from vc120 in libraries name).
You must build boost with MinGW-64 (you can use the same stage/lib folder because names are different).
Open the MinGW console and follow the same compilation step that you use for Visual Studio, but change toolset from msvc to gcc.