g++ compile error: undefined reference to a shared library function which exists - g++

I recently installed the hdf5 library on an ubuntu machine, and am now having trouble linking to the exported functions. I wrote a simple test script readHDF.cpp to explain the issue:
#include <hdf5.h>
int main(int argc, char * argv[])
{
hid_t h5_file_id = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
return 0;
}
The compile command is
g++ -Wl,-rpath,$HOME/hdf5/lib -I$HOME/hdf5/include \
-L$HOME/hdf5/lib -l:$HOME/hdf5/lib/libhdf5.so readHDF.cpp
which returns the following error
/tmp/cc6DXdxV.o: In function `main':
readHDF.cpp:(.text+0x1f): undefined reference to `H5check_version'
readHDF.cpp:(.text+0x3c): undefined reference to `H5Fopen'
collect2: ld returned 1 exit status
I am confused because the nm command seems to say that the function has been exported:
nm -C $HOME/hdf5/lib/libhdf5.so | grep H5check_version
which returns
0000000000034349 T H5check_version
and a similar result for H5Fopen. Any thoughts on what might be going wrong? Not sure if it helps, but if I comment out the H5Fopen portion of the script, then it compiles fine:
#include <hdf5.h>
int main(int argc, char * argv[])
{
hid_t h5_file_id;// = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
return 0;
}
Also there are multiple versions of hdf5 installed on the server which are used by various python modules such as h5py and tables, but I couldn't get any of them to work, so I installed this version in my local directory and changed the rpath options for g++ linker.

Ok, solved. The issue was in the placement of the -lhdf5 in the compile command. Apparently -lhdf5 should be placed after readHDF.cpp. For instance g++ -Wl,-rpath=$HOME/hdf5/lib -L$HOME/hdf5/lib -I$HOME/hdf5/include readHDF.cpp -lhdf5 will compile with no problems, but g++ -Wl,-rpath=$HOME/hdf5/lib -L$HOME/hdf5/lib -I$HOME/hdf5/include -lhdf5 readHDF.cpp will fail with the undefined reference errors. Interestingly, this was only a problem for Ubuntu 12.04, as both compile commands worked for Ubuntu 10.04.
Found the answer with explanation at this post:
undefined reference to symbol even when nm indicates that this symbol is present
I guess placing -lXXX after the script is safer practice.

This is not a bug. See
C++ shared library undefined reference to `FooClass::SayHello()'
"Recent versions of GCC reuqire that you put the object files and libraries in the order that they depend on each other ..."

You forgot to put -lhdf5 in the compile command. Also, there's no need for -l:$HOME/hdf5/lib/libhdf5.so
This should work: $ g++ -Wl,-rpath,$HOME/hdf5/lib -I$HOME/hdf5/include -L$HOME/hdf5/lib -lhdf5 readHDF5.cpp

Related

undefined symbol: __afl_area_ptr when trying to compile Kisak strike

I want to fuzz the map loading feature in counter strike. The source code for csgo got leaked a while back and here is a build which you can compile https://github.com/SwagSoftware/Kisak-Strike . However, when I try to compile it using this command:
AFL_USE_ASAN=1 PERSIST=1 cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_ROCKETUI=ON -DFREETYPE_LIBRARY=/usr/lib/x86_64-linux-gnu/libfreetype.so -DFREETYPE_INCLUDE_DIRS=/usr/include/freetype2/freetype/ -DUSE_KISAK_PHYSICS=ON -DCMAKE_C_COMPILER=/home/cyberhacker/Asioita/Hakkerointi/Counterstrikestuff/aflpp/afl-gcc-fastthing/AFLplusplus/afl-gcc-fast -DCMAKE_CXX_COMPILER=/home/cyberhacker/Asioita/Hakkerointi/Counterstrikestuff/aflpp/afl-gcc-fastthing/AFLplusplus/afl-g++-fast ..
and then when i try to run ./csgo_linux64 , it throws this error:
Failed to load the launcher(bin/linux64/launcher_client.so) (/home/cyberhacker/Asioita/Csgocompile/withjump/game/bin/linux64/libtier0_client.so: undefined symbol: __afl_area_ptr)
I am expecting it to just work as usual (same as with the regular build). It works completely fine when I try to compile it normally with this command:
cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_ROCKETUI=ON -DFREETYPE_LIBRARY=/usr/lib/x86_64-linux-gnu/libfreetype.so -DFREETYPE_INCLUDE_DIRS=/usr/include/freetype2/freetype/ -DUSE_KISAK_PHYSICS=ON -DCMAKE_C_COMPILER=/usr/bin/cc -DCMAKE_CXX_COMPILER=/usr/bin/c++ ..
and then make -j8
I searched a bit and found this: this . In that conversation they say that this error can be cause by compiling with afl-gcc but then linking with normal gcc or ld. However this is not the cause of my problem since I have looked through the cmake log files and the binaries are being linked with afl-g++-fast so this shouldn't be a problem in my case. I read somewhere that the libraries should be statically included, not dynamic but I do not think that it is a necessity. Feel free to look at the Kisak strike source code if you want to (obviously).

clang-tidy report error unknown argument when contain other compiler options

I have a project, I built it with intel compiler. I want use the clang-tidy to help detect code problems.
I am using CMake to generate compile_commands.json and I'm getting the follow error when I using clang-tidy:
$ run-clang-tidy
# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 warning and 1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]
warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [clang-diagnostic-unknown-warning-option]
Actually, there is a very simliar question exist: clang-tidy reporting unknown warnings
However, when I try to using the method refered above, there is no help. The warning can be suppressed but error still exist.
$ run-clang-tidy -extra-arg=-Wno-unknown-warning-option
# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -extra-arg=-Wno-unknown-warning-option -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]
How can I deal with the error?
-w2 options is used to control warning in intel compiler.
Although the problem occurs to me because of the intel compiler, but may be other compiler's options can also leads to the problem.
Appendix
The follow code snippets can help reproduce the problem.
// CMakeLists.txt
SET(CMAKE_CXX_COMPILER "icc")
SET(CMAKE_CXX_COMPILER "icpc")
project(test)
# leads to warning, can be settled by refer link
add_compile_options("-Wno-maybe-uninitialized")
# leads to error, can not be settled by refer link
add_compile_options("-w2")
add_executable(a.out main.cpp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
// main.cpp
#include <iostream>
int main()
{
std::cout << "hello!" << std::endl;
return 0;
}
the above code can generate compile_commands.json like follows:
[
{
"directory": "/home/xuhui/temp/build",
"command": "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc -Wno-maybe-uninitialized -w2 -o CMakeFiles/a.out.dir/main.o -c /home/xuhui/temp/main.cpp",
"file": "/home/xuhui/temp/main.cpp"
}
]
Thanks for your time.
This is not a clang-tidy error per se. Clang-diagnostic-error is basically a compiler error. Clang has made unknown arguments a hard error some time ago and it cannot be degraded to a warning. There used to be -Qunused-arguments but that doesn't work in Clang 11 AFAIK.
You will have to remove the argument before passing the compile commands to clang-tidy, I suggest CMake - remove a compile flag for a single translation unit.
#pablo285 already give the perfect answer.
He said that:
have to remove the argument before passing the compile commands to
clang-tidy
He already provide a link to demonstate how to modify CMakeLists.txt to remove argument.
Also, we can do some modification directly on compile_commands.json to remove argument.
A script to tidy code can be written as follows:
# tidy_code.sh
cd build
cmake ..
# do modification on compile_commands.json to remove argument which clang can not recognized
# replace '-w2' to ' '
sed -i 's/-w2/ /g' compile_commands.json
# using clang tidy
run-clang-tidy -checks='*' -extra-arg=-Wno-unknown-warning-option

wxwidget build failed - windows - mingw - mingw32-make: *** [..\..\lib\gcc_dll\wxmsw313u_gcc_custom.dll] Error 1

I was trying use build wxWidgets-3.1.3 with MinGW-W64 on a x64 windows machine.
I followed this thread, which lead me to download and building. So I installed it and some youtube videos said I need to build it now. So navigate to the installed folder and gave this command :
mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=1 UNICODE=1.
It took almost half an hour and now it's giving me error saying :
collect2.exe: error: ld returned 1 exit status
makefile.gcc:5329: recipe for target '..\..\lib\gcc_dll\wxmsw313u_gcc_custom.dll' failed
mingw32-make: *** [..\..\lib\gcc_dll\wxmsw313u_gcc_custom.dll] Error 1
Here is the full log file :
https://pastebin.com/zxeHhF6K
MinGW configuration :
Version : 8.1.0
Architecture : x86_64
Threads : posix
Exceptions : seh
Build version : 0
How can I solve this? I'm using CLion, is there any other short or easy way?
The relevant error is
..\..\lib\gcc_dll/libwxexpat.a(wxexpat_xmlparse.o):xmlparse.c:(.text+0x337d): undefined reference to `_imp__rand_s'
and it's very strange because MinGW-w64 8.1 is definitely supposed to have rand_s(). Are you sure you're using the right compiler? I.e. what does g++ -v give you if you run it from the same command prompt?
My only hypothesis is that it's some different (and much older) compiler and the solution would be to just use the right one instead.
Also, the next time you could use -j4 option with make if you have at least 4 logical CPUs in your machine (and chances are you do nowadays), to significantly speed up the build.
Looking back in my notes I once had an issue with missing rand_s() when building glib2 on a certain MinGW build.
I was able to fix it then by adding this at the top of the C file that called this function:
#include <time.h>
#include <stdlib.h>
int rand_s (unsigned int* r)
{
static int srand_called = 0;
if (!srand_called) {
srand(time(0));
srand_called++;
}
if (r)
*r = rand();
return 0;
}
In your case that would be in xmlparse.c.

GCC and -fsanitize=leak

I'm working and on a large C++ project and making it compile with clang would be painful, so I'm stuck with GCC.
I want to use the nice -fsanitize=leak flag that I already used with clang on a previous job, but it does not seem to work.
I made a very simple example to test it:
#include <stdlib.h>
void FooBar() {
malloc(7);
}
int main() {
FooBar();
return 0;
}
With clang it works as expected:
>> clang -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==18052==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x41dcbc (~/dev/addresssanitizertest/a.out+0x41dcbc)
#1 0x431ac3 (~/dev/addresssanitizertest/a.out+0x431ac3)
#2 0x431ae3 (~/dev/addresssanitizertest/a.out+0x431ae3)
#3 0x7f8077e71a3f (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)
SUMMARY: LeakSanitizer: 7 byte(s) leaked in 1 allocation(s).
>>
But with gcc it does not seem to detect anything:
>> gcc -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
>>
Did I miss something like a nice environment variable? Did someone ever made it work with gcc?
EDIT: This works for instance:
g++ -fsanitize=address main.cpp
ASAN_OPTIONS=detect_leaks=1 ./a.out
But I can't do that: the perf drawback is too much. I only want leak detection.
You must read this and use the patch :
https://gcc.gnu.org/ml/gcc-patches/2013-11/msg01874.html
I resolved it with gcc 5.1 (I was using 4.9).
EDIT: it looks like 5.2 does not work either
EDIT2: it does not work with the gcc provided with ubuntu 15.10 (5.2.1), however I recompiled a 5.2.0 from sources and it worked fine. I really have no clue.
I had the same issue (with Ubuntu 15.04 and gcc 4.9.2).
Using ldd I noticed that liblsan.so was not linked, so here how I fixed it :
1) Force linking during compilation
>> gcc -fsanitize=leak main.cpp -llsan
2) Execute with
./a.out

How to create and load a GTK module?

I want to create a custom GTK module which should be loaded when I start a GTK application.
Documentation on this topic is rare, I searched a lot but I failed to get it running. I'm on Ubuntu Linux with GTK3 and tried sofar:
Compiled and linked a shared library with the method void gtk_module_init(gint *argc, gchar ***argv[]) inside. As far as I understood, this should be enough to create a simple module. Full code:
#include <iostream>
#include <gtk/gtk.h>
void gtk_module_init(gint *argc, gchar ***argv[]) {
std::cout << "huhu" << std::endl;
}
Put this lib into /usr/lib/x86_64-linux-gnu/gtk-3.0/modules/libtest-gtk-module.so
Tried to launch an application like this: gnomine --gtk-module=libtest-gtk-module.so But all I get is: Gtk-Message: Failed to load module "libtest-gtk-module.so"
So what else has to be done in order to make GTK load this library?
Many thanks in advance!
You need to make the system aware of the library. For a library in a system directory, it should be enough to run ldconfig as root. Take a look at the tutorial here.
[EDIT]
I got the module to load as follows:
Since this is C++ code, you need to make sure the function name isn't name mangled:
extern "C" {
void gtk_module_init(gint *argc, gchar ***argv[]) {
std::cout << "huhu" << std::endl;
}
}
I built it with the following:
g++ -fPIC -shared -Wl,-soname,libfoo.so.1 -olibfoo.so.1.0.1 `pkg-config --libs --cflags gtk+-3.0` t.c
I used an absolute path to avoid messing with ldconfig, this is probably the best thing to do while developing the module:
~$ gedit --gtk-module=/home/eric/libfoo.so.1.0.1 t.c
huhu
This is on Mint LMDE, not Ubuntu, but I don't think it matters.
Try to run it with strace:
strace -eopen add_your_command_here 2>&1 | grep libtest-gtk-module.so
You'll find out where your program is looking for that file.