How use llvm linker? - c++

LLVM provides 2 tools llvm-link and llvm-ld. I would like to know:
how merge all .o file in one ?
how set a soname like with gcc -Wl,-soname,libsomething.so.1 ?
I would like do this in c++ but if show to me how do that from command line i will found how do to do in c++.
thanks

llvm-link is a tool for linking (~ merging) LLVM IR files into another LLVM IR file.
llvm-ld tries to be compatible to ld. Note that LLVM currently has no real linking capabilities, so llvm-ld calls gcc to do the actual final stages.
Note that if you just want to have GCC's functionality, use the clang driver:
clang -c file.c -fpic
clang -shared file.o -o file.so
You can also pass the -Wl flags to clang as you'd do for gcc:
clang -shared file.o -Wl,-soname,libfile.so.8 -o file.so

Related

MinGW, how to avoid linking statically full libstdc++

I am using mingw 64 bit with cygwin.
I know that if I compile using
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp
the output .exe does not run unless the library path to libstdc++ and other libraries is specified in the Path environment variable.
An alternative is to link statically
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread
Since I want a single .exe that I can easily copy on different machines, the second solution is better for me. My only problem is that, since I link statically, even for a simple helloworld program, the size of the executable rises to more than 10 Mb. So my question is: is it possible to link statically only the library parts that are actually used by the program?
The binutils linker on Windows, ld, does not support the --gc-sections argument properly, which in combination with compiler flags -ffunction-sections and -fdata-sections, allow the linker to throw away blocks of unused code.
You are straight out of luck. The only thing you can do is the usual: strip the executable (by running the strip command on it after it is linked) and compile your code optimising for size with -Os.
Keeping in mind these options do not work on Windows (which for the purpose of this answer, includes the Cygwin platform), this is generally how you can do this:
g++ -c -Os -ffunction-sections -fdata-sections some_file.cpp -o some_file.o
g++ -c -Os -ffunction-sections -fdata-sections main.cpp -o main.o
g++ -Wl,--gc-sections main.o some_file.p -o my_executable

Unable to link PAPI library with opt llvm

I am working on a project where I need to generate just the bitcode using clang, run some optimization passes using opt and then create an executable and measure its hardware counters.
I am able to link through clang directly using:
clang -g -O0 -w -I/opt/apps/papi/5.3.0/include -Wl,-rpath,$PAPI_LIB -L$PAPI_LIB \
-lpapi /scratch/02681/user/papi_helper.c prog.c -o a.out
However now I want to link it after using the front end of clang and applying optimization passes using opt.
I am trying the following way:
clang -g -O0 -w -c -emit-llvm -I/opt/apps/papi/5.3.0/include -Wl,-rpath,$PAPI_LIB -L$PAPI_LIB \
-lpapi /scratch/02681/user/papi_helper.c prog.c -o prog.o
llvm-link prog.o papi_helper.o -o prog-link.o
// run optimization passes
opt -licm prog-link.o -o prog-opt.o
llc -filetype=obj prog-opt.o -o prog-exec.o
clang prog-exec.o
After going through the above process I get the following error:
undefined reference to `PAPI_event_code_to_name'
It's not able to resolve papi functions. Thanks in advance for any help.
Clearly, you need to add -lpapi to the last clang invocation. How else the linker would know about libpapi?

How to link a library with clang and llvm-link

I am compiling my program like this,
clang++ -O4 -emit-llvm file1.cpp -c -o file1.bc -pthread
clang++ -O4 -emit-llvm file2.cpp -c -o file2.bc -pthread
llvm-link file1.bc file2.bc -o main.ll -S
How do I specify linking with -ldl
llvm-link is a program which "links" together LLVM IR files into a single IR file; you can read more about it here. It does not have any relation to ld or to linking object files together.
If you do want to generate object code and/or executables, see these related questions:
How to generate machine code with llvm
llvm-link with external libraries
In short, you should be using native tools for assembling and linking (as and ld, for instance), though there is currently some experimental support for generating object files and for linking in LLVM.
In any case, Clang itself can invoke a platform linker - that is actually the default, but of course you have overridden by providing -c.

how to run two executable generated with different gcc version in same system

I have two Executable generated with different gcc version
One is using gcc 3.4.2 and other using gcc 4.3.2 in my Linux box
Both has to be run in same environment i.e. having same LD_LIBRARY_PATH.
Currently path of 4.3.2 is placed before the 3.4.2 the 3.4.2 is giving error.
libstdc++.so.6: version 'GLIBCXX_3.4.9' not found (required by../../src/hello)
I am thinking about the solution where I can store the information in the exe where to find the
loadtime files needed.
I created below build scripts which are giving problem:
Basically O3 option is doing optimization.
/opt/gcc-4.3.2/bin/g++ -pipe -O3 -c hello4_3_2.cpp
/opt/gcc-4.3.2/bin/g++ -o hello4_3_2 hello4_3_2.o -L$/opt/gcc-4.3.2/lib64/libstdc++
/opt/gcc-3.4.2/bin/g++ -pipe -O3 -c hello3_4_2.cpp
/opt/gcc-3.4.2/bin/g++ -o hello3_4_2 hello3_4_2.o -L$/opt/gcc-3.4.2/lib64/libstdc++
Below script works for me:(With out O3 option)
/opt/gcc-4.3.2/bin/g++ -pipe -c hello4_3_2.cpp
/opt/gcc-4.3.2/bin/g++ -o hello4_3_2 hello4_3_2.o -L$/opt/gcc-4.3.2/lib64/libstdc++
/opt/gcc-3.4.2/bin/g++ -pipe -c hello3_4_2.cpp
/opt/gcc-3.4.2/bin/g++ -o hello3_4_2 hello3_4_2.o -L$/opt/gcc-3.4.2/lib64/libstdc++
Now:
I wanted to know if there is any other way to achieve it.
Is there is any draw back of doing in this way.
Specify an rpath when linking:
/opt/gcc-4.3.2/bin/g++ -o hello4_3_2 hello4_3_2.o -Wl,-rpath,/opt/gcc-4.3.2/lib64
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This will hardcode a library search path into the executable.
You can use ldd ./hello4_3_2 to check without running whether the correct libraries are being found.
The libstdc++ manual lists several options

Compiler not creating an executable

I was recompiling the existing source files but it is not able to create the executable though it file generated has execute permission
-rwxrwxr-x 1 ilvweb ilv 2949112 Jan 31 09:34 karny
$ file karny
karny: ELF 64-bit MSB dynamic lib SPARCV9 Version 1, dynamically linked, not stripped
The command used for compilation
g++ -m64 -mcpu=v9 -DSUNOS -DNDEBUG -g -Wno-deprecated -o karny tkm.o THlm.o Connection.o Socket.o ThLogger.o File.o TextFile.o File.o Timer.o lPlugin.o \
-G -lm
Is there any option missing
If we have both gcc and g++ and the source written for g++ in order to recompile with gcc what could be done as the as the currently in this environment we can use only gcc
According to the GCC manual:
3.17.41 Options for System V
These additional options are available on System V Release 4 for
compatibility with other compilers on those systems:
-G
Create a shared object. It is recommended that -symbolic or -shared be used instead.
[... snip ...]
Seeing as you're on (SysV-derived) Solaris, it would seem you're asking GCC to create a shared object. Is there a specific reason you have that -G?
According to file, you made a shared object (which usually would be named *.so).