LLVM Adding a pass - linking error - llvm

I have written my pass in llvm/lib/Transforms, and its called createABCDPass. I have added the following code in my pass:
namespace llvm { FunctionPass *createABCDPass(); }
FunctionPass *llvm::createABCDPass() { return new AbcRemoval(); }
where AbcRemoval is the class of the pass.
After that, I have done a forward declaration in lib/CodeGen/LLVMTargetMachine.cpp in order to recognize my pass:
namespace llvm { FunctionPass *createABCDPass(); }
PM.add(createABCDPass());
However, when I run make on llvm, i get the following error:
llvm[2]: Linking Release executable llc (without symbols)
Undefined symbols:
"llvm::createABCDPass()", referenced from:
llvm::LLVMTargetMachine::addCommonCodeGenPasses(llvm::PassManagerBase&, llvm::CodeGenOpt::Level, bool, llvm::MCContext*&)in libLLVMCodeGen.a(LLVMTargetMachine.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [/Users/.../llvm/Release/bin/llc] Error 1
make[1]: *** [llc/.makeall] Error 2
make: *** [all] Error 1
Does anybody know why am I getting this error? Thanks!

Ah, I fixed it in the end by renaming the pass module to -libLLVM_xxx. Apparently you got to name it libLLVM_"something" in order for it to run with all the other passes in LLVM dynamically. Not sure why, but it works!

You have to link your pass to llc. By default llc pulls almost nothing from lib/Transforms, so your pass won't be linked in to llc.

Related

I got an "cannot find -lOpenGL::OpenGL" error when make install partio

When I install the partio it shows the following error message
/usr/bin/ld: cannot find -lOpenGL::OpenGL
collect2: error: ld returned 1 exit status
src/tools/CMakeFiles/partview.dir/build.make:91: recipe for target 'src/tools/partview' failed
make[2]: *** [src/tools/partview] Error 1
CMakeFiles/Makefile2:967: recipe for target 'src/tools/CMakeFiles/partview.dir/all' failed
make[1]: *** [src/tools/CMakeFiles/partview.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
When I search the internet I barely see any information regard to -lOpenGL::OpenGL. I have seen OpenGL::GLU or GL. But I havent seen OpenGL::OpenGL yet. So I don't know how to solve this.
I am using the cmake version exactly 3.15.0.
Is there any solution to this? Anything would help.
Welcome to Stack Overflow!
The library you're looking for is called libGL.so, not libOpenGL.so or similar. To link it, add -lGL to your command line and delete references to OpenGL::OpenGL on the command line. (If you're still having troubles, show us your whole build command line.)
That said, OpenGL is a C-based library, so C++ constructs like OpenGL::OpenGL (presumably a constructor of a class) will not be found therein. Or perhaps that's a reference to CMake, in which case my answer would need to change significantly.

Errors on compilation

I tried to complile this project: https://github.com/ccshiro/corecraft
I am use Ubuntu 16.04, i have installed: gcc 4.9, 5.0, 6.0; g++ 4.9, 5.0; clang; cmake3; and libsparsehash-dev .
I got this error:
[ 96%] Linking CXX executable mangosd
../game/libgame.a(Map.cpp.o): In function `sh_hashtable_settings<ObjectGuid, std::tr1::hash<ObjectGuid>, unsigned long, 4>::hash(ObjectGuid const&) const':
/usr/include/google/sparsehash/hashtable-common.h:65: undefined reference to `std::tr1::hash<ObjectGuid>::operator()(ObjectGuid) const'
collect2: error: ld returned 1 exit status
src/mangosd/CMakeFiles/mangosd.dir/build.make:244: recipe for target 'src/mangosd/mangosd' failed
make[2]: *** [src/mangosd/mangosd] Error 1
CMakeFiles/Makefile2:930: recipe for target 'src/mangosd/CMakeFiles/mangosd.dir/all' failed
make[1]: *** [src/mangosd/CMakeFiles/mangosd.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
Here Map.cpp , here /usr/include/google/sparsehash/hashtable-common.h
I ve tried to google about "collect2: error: ld returned 1 exit status" error, and found that in code might be cyrrilics or non latinics symbols, but i didnt find something wrong in this 2 files above.
On issue tracker i also found same error from another person https://github.com/ccshiro/corecraft/issues/5
I am not C++ programmer so i cant understand what wrong is here, can anyone help me with this?
What you are seeing is a linker error. Everything compiles fine, and then when the linker starts to stitch the code together it's missing a object that defines the functionality of the std::tr1::hash<ObjectGuid>::operator() hash operator. This is a template specialization that allows this object to be used as a unique key in a map (or hash set).
The template for the hash function is specified here. At first glance, I wasn't seeing why it shouldn't link, but then I realized that the linker is looking for std::tr1::hash<ObjectGuid> instead of std::hash<ObjectGuid>. Basically, it looks like your STL library is using TR1 which is an older pre-standard version of C++11.
Your first attempt should be to figure out how to specify that your compiler uses a newer version of the STL library. You should be able to add -std=c++11 to the CMAKE C++ flags (instead of -std=c++0X). Whether this means editing the CMakeLists.txt file to include the flag or making sure that your compiler was installed with a more modern version of STL.
That should fix the problem. I can think of another solution, but I suspect that you will get more errors by linking to an older version of STL.

recompile with -fPIC flag

I have a project (not mine) which I want to build. It has c++ and Python modules, which has to bind. When I launch cmake, I get this error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libantlr.a(BitSet.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libantlr.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [python/CModule.so] Error 1
make[1]: *** [pyBinding/CMakeFiles/CMODULE.dir/all] Error 2
make: *** [all] Error 2
I install antlr library with apt-get, and as I understand, I should recompile this library, but I have no idea in which part of source code of antlr library I should put -fPIC, or is there another way?
In Ubuntu 14.10 I have package libantlr-dev, which contains libantlr.a as well as libantlr-pic.a. I believe you have to use latter one, it is compiled with PIC

Issue with CMake project building

I have the following problem. On my Ubuntu I try to build a project and receive the following linker error(s) so far:
/usr/bin/ld:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libboost_thread.a(once.o): undefined reference to symbol 'pthread_once##GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO
missing from command line collect2: error: ld returned 1 exit status
make[2]: *** [sunprint] Error 1 make[1]: ***
[CMakeFiles/sunprint.dir/all] Error 2 make: *** [all] Error 2
*** Failure: Exit code 2 ***
I'm running under ubuntu 13 desktop, GCC 4.8, boost ver. is 1.54. As an IDE I'm working with
is the KDevelop.
I can provide any additional info about this issue if needed, but now I'm stuck with
this linking problem.
Any ideas?
Thanx in advance.
add_definitions only adds inputs for the preprocessor, which is in action even before the compiler starts its business and even much farther away from linking the executable, the step ld is supposed to be doing.
What you want to have ld resolve link-time dependencies is the CMake command target_link_libraries, which, for a given target, add a number of libs to link against after compilation.
In you case, the appropriate invocation could look like this
target_link_libraries(${PROJECT_NAME} [...] -lpthread [...]) #obviously without the '[...]' and the correct target name
I had a similar problem but with mpich. Both:
target_link_libraries(${PROJECT_NAME} [...] -lmpich [...])
and
target_link_libraries(${PROJECT_NAME} [...] mpich [...])
worked properly.

undefined reference linking yaml-cpp program with mingw-w64 + cmake

I'm using mingw-w64 (4.8.0) + cmake to build shared libraries for yaml-cpp (0.5.1). The project has a few build targets in the CMakeLists.txt that comes with it: the main library yaml-cpp and some test programs e.g. parse, which links the library.
The library target yaml-cpp itself built without error but it is followed by this error when parse is built:
Linking CXX executable parse.exe
CMakeFiles\parse.dir/objects.a(parse.cpp.obj):parse.cpp:(.text+0x1a3): undefined reference to `YAML::Load(std::istream&)'
CMakeFiles\parse.dir/objects.a(parse.cpp.obj):parse.cpp:(.text+0x1b2): undefined reference to `YAML::operator<<(std::ostream&, YAML::Node const&)'
c:/work/mingw64-4.8/bin/../lib/gcc/x86_64-w64-mingw32/4.8.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\parse.dir/objects.a(parse.cpp.obj): bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status
util\CMakeFiles\parse.dir\build.make:90: recipe for target 'util/parse.exe' failed
mingw32-make[3]: *** [util/parse.exe] Error 1
CMakeFiles\Makefile2:228: recipe for target 'util/CMakeFiles/parse.dir/all' failed
mingw32-make[2]: *** [util/CMakeFiles/parse.dir/all] Error 2
CMakeFiles\Makefile2:240: recipe for target 'util/CMakeFiles/parse.dir/rule' failed
mingw32-make[1]: *** [util/CMakeFiles/parse.dir/rule] Error 2
makefile:211: recipe for target 'parse' failed
mingw32-make: *** [parse] Error 2
I run into similar errors when I try to write small test programs using the library, and I don't know what is wrong. What might be the issue here?
Jesse Beder's comment points to the exact issue causing the error described in the question. A quick fix while this gets fixed in a follow-up release is to apply the patch (https://code.google.com/p/yaml-cpp/issues/detail?id=216#c4) to a clean copy of yaml-cpp 0.5.1. There is also a line that declares std::string node_data::empty_scalar in the source file src/node_data.cpp that should be commented out. After making these changes, the shared libraries and utilities build without error.