Make(compiler) complains about missing llvm/PassAnalysisSupport.h file - cmake

I downloaded LLVM (12.0.1) from the script file llvm.sh which they have now started to include on their Debian downloads page.
Ran clang and everything was working as expected(good job dev team I guess?).
The installation script installed LLVM in /usr/lib/LLVM-12/ and /usr/include/LLVM-12/ (these had some .h files)
So I tried writing some passes; created an out-of-tree build using the answer given here:
Facing issue with makefile for Hello pass in llvm
But when I ran the make, it gave me this error:
/usr/include/llvm-12/llvm/Pass.h:337:10: fatal error: llvm/PassAnalysisSupport.h: No file or directory
337 | #include "llvm/PassAnalysisSupport.h"
|
compilation terminated.
So I checked /usr/include/llvm-12/llvm/ and found that Pass.h and PassAnalysisSupport.h both were present in the current directory, so shouldn't the Pass.h file have #include "PassAnalysisSupport.h" instead of the present #include "llvm/PassAnalysisSupport.h"?
I also checked the code for Pass.h online and it also had #include "llvm/PassAnalysisSupport.h". Other header files in the llvm directory also used the same format #include "llvm/<name>"
So what is going on here, who messed up, the devs or my llvm.sh (also the devs) or the problem is something else?
Any help would be appreciated (Im using Mint MATE 20.2 if thats relevant)
Source file (headers; firstpass/first/fpass.cpp):
#include <llvm-12/llvm/Pass.h>
#include <llvm-12/llvm/IR/Function.h>
#include <llvm-12/llvm/Support/raw_ostream.h>
#include <llvm-12/llvm/IR/LegacyPassManager.h>
#include <llvm-12/llvm/Transforms/IPO/PassManagerBuilder.h>
(firstpass/CMakeLists.txt):
find_package(LLVM REQUIRED CONFIG)
include_directories($(LLVM_INCLUDE_DIRS))
add_subdirectory(first)
(firstpass/first/CMakeLists.txt):
add_library(LLVMfirst MODULE fpass.cpp)

Related

clang++ compiles an executable, but clang-check cannot find std header

I have a test cmake C++ app, that I can compile successfully with clang++-10 on Ubuntu 20.04 (with all that CMAKE_USER_MAKE_RULES_OVERRIDE machinery).
The layout of the cmake project is:
./test.cpp
./CMakeLists.txt
./build
I also create the build database json file with CMAKE_EXPORT_COMPILE_COMMANDS inside ./build (this json file is necessary for the clang-check operation AFAIU).
Now, launched from the build dir, the check invocation fails:
clang-check-10 --analyze ../test.cpp
<projDir>/test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
Error while processing <projDir>/test.cpp.
Any ideas how to fix this?
Edit: Have also tried specifying -p=$(pwd) to clang-check, but it still could not find the header.
clang-check is not a compiler. It's used for checking error in AST, and in this case it can't find the system header in your code. Add '--' in the end of line to ignore it.
clang-check-10 --analyze ../test.cpp
For more information: ClangCheck on LLVM

Command line to build C++ program with LLVM libs

I am starting in the world of LLVM and searched in several places and read several documentation about LLVM but I found nothing showing how to compile a program that uses LLVM headers and libs ....
I wrote this simple program just to try to compile, using the Visual Studio cross-compiler, I tried several command line options .... even using the -lLLVM option, but, nothing worked ...
I tried using g++ and clang++
#include <iostream>
#include <llvm/ADT/OwningPtr.h>
#include <llvm/Support/MemoryBuffer.h>
int main()
{
llvm::OwningPtr<llvm::MemoryBuffer> buffer
return 0;
}
When I try to build, I get this erro:
error : 'llvm/ADT/OwningPtr.h' file not found
So, what is the command line to compile this simple program?
The command llvm-config --cxxflags --ldflags --system-libs --libs core will provide you with all the linkable llvm libraries, provided you have llvm installed. Just link with this command in single quotes

cmake configure_file just for update a line not over-writing the whole file

I know that we can configure a file using cmake as discussed in Can CMake generate configure file? for example.
My problem is that I want to update just one line of a file. For instance, assume that this line contains app version:
my_header_version.h.in:
#include "${CMAKE_SOURCE_DIR}/src/definitions.h"
CMakeLists.txt:
...
configure_file(my_header_version.h.in my_header_version.h)
...
my_header_version.h:
#include "/home/myusr/dev/src/definitions.h"
The problem is that if I change my_header_version.h into:
#include <some_lib.h>
#include "/home/myusr/dev/src/definitions.h"
and after that I run cmake .. command, then the line #include <some_lib.h> gets lost.
What should I do if I want to change my_header_version.h directly without changing my_header_version.h.in anytime?!

Plugin with own library kills Browser

I am new to C++ and plugin development. I am working with/for Unix and for the Firefox browser. So here we go:
I have a plugin which uses some classes from a own library. The problem is: it kills my browser asap. I cant even start my browser as soon as MyPlugin.so is in the plugin folder of the Firefox. The library is build and doesn't kill a desktop application that uses it.
My guess is that I failed at linking my library with CMake or forgot to include some stuff from FireBreath. So here are the two things I assume are wrong, maybe someone can help me out.
1) (wrong?) linking with Cmake:
I added some of these at the end of the CMakeLists.txt of my project. The paths are where the library is.
add_definitions(-L${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/bin)
add_definitions(-I${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/src)
add_definitions(-lcoala64) [name of the library]
add_definitions(-Wl,-rpath=${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/bin)
add_definitions(-pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/harfbuzz -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0)
And used the prepmake.sh to generate my build files. Then I followed up with adding flags manually (because I dont know a better solution) to the in the /buid/projects/MyPlugin/CMakeFiles/MyPlugin.dir/link.txt
-L/home/username/swp/dev/lib/bin
-I/home/username/swp/dev/lib/src
-lcoala64 -Wl,-rpath=/home/username/swp/dev/lib/bin
Afterwards I could build the plugin. It builds, so one could assume I have linked correctly. But said crashes appear as soon as I want to use it.
2) Do I use the library wrong?
I include like this in MyPluginAPI.h:
#include <string>
#include <sstream>
#include <boost/weak_ptr.hpp>
#include <boost/smart_ptr.hpp>
#include "JSAPIAuto.h"
#include "BrowserHost.h"
#include "X11/X11KryptoKoala.h"
//Include from my own library:
#include "../../../lib/src/Key.hpp"
As soon as I add the following line to MyPlugin.cpp I get the mentioned crashes while the same line works without a problem in the desktop application that uses the same library:
Key key(password_);
Now I hope this isn't a too big wall of text and someone is willing to investigate and answer to me.
You shouldn't use add_definitions() in that way. CMake allows to differentiate your directives in different categories, so that they only go in the necessary command line. You should use:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
include_directories(/usr/include/gtk-2.0
/usr/include/cairo
etc. etc.
)
add_library(the_name_of_your_target gtk-x11-2.0 gdk-x11-2.0 ETC. ETC.)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../lib/bin)
Furthermore, there are FindPackage functionalities that can help you setting automatically variables containing the name of your libraries, their directories, their include path.
Most information can be found here and here
Then: What is then prepmake.sh? Are you running cmake at all? Can you use cmake-gui, and then select one canonical build system, like make or ninja?
Finally: It could be that you have a crash because your library are not in your library path. I assume you are under linux, here are some ideas: LD_LIBRARY_PATH vs LIBRARY_PATH and LD_LIBRARY_PATH

Why am I unable to #ifdef stdafx.h?

I am trying to include 2 platform-specific stdafx.h files in my .cpp file, but the compiler is unhappy when I try to #ifdef it.
#ifdef _WIN32
#include "stdafx.h"
#elif _MAC
#include "MAC/stdafx.h"
#endif
You may wonder why I am using stdafx.h in the Mac code, but that is not important at the moment :).
When I try to compile the code on Windows, I receive: Fatal Error C1018. I tried enclosing other header files with #ifdef in the same file, and the compiler was happy. Therefore, it looks like Windows doesn't like stdafx.h to be #ifdef-ed, or that Windows only allows #include stdafx.h
to be the first line in the file.
So my question is, why?
Kat
When the compiler includes a pre-compiled header, it basically "forgets" anything that came before the header. Thus your #elif isn't matched to a #if anymore.
This is because you have Precompiled Headers turned on - turn it off and you should be fine.