Recently I need to use zlib in programming on Ubuntu and here I have a problem.
I wrote a small program to test zlib functions and I built it in Eclipse.
I found it can be compiled but there were some linking errors like this:
main.cpp:27: undefined reference to `compress'
main.cpp:38: undefined reference to `uncompress'
What should I do with this? If this is because there is no library file?
Can anyone give some help? Thanks a lot!
MOODY_Y
Plus, here are my building info:
11:59:08 **** Build of configuration Debug for project test_zlib ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: test_zlib
Invoking: GCC C++ Linker
g++ -o "test_zlib" ./main.o
./main.o: In function `main':
/home/hyq/workspace/test_zlib/Debug/../main.cpp:27: undefined reference to `compress'
/home/hyq/workspace/test_zlib/Debug/../main.cpp:38: undefined reference to `uncompress'
collect2: ld return 1
make: *** [test_zlib] error 1
11:59:10 Build Finished (took 1s.715ms)
Try:
sudo apt-get install zlib1g-dev
Also, use -lz on the compile/link command.
You need to link youe source to zlib.Build your code as follows
gcc main.cpp -lz
Related
Based on this question i get error message...
collect2: fatal error: ld terminated with signal 6 [Abgebrochen]
compilation terminated.
/usr/bin/ld: ld: wcsrtombs.c:99: __wcsrtombs: Zusicherung »data.__outbuf[-1] == '\0'« nicht erfüllt.
...when trying to link a shared library but setting LC_ALL=C doesn't solve the problem in my case!
I'm on a german Linux Mint 17.3 (Cinnamon). My project gets build with CMake 2.8.4 (native repository). Compiler is g++ 5.3.1 (Ubuntu xenial repository).
Compile command for all sources looks like this:
/usr/bin/c++ -D_USE_MATH_DEFINES -Dfluag_EXPORTS -g -fPIC -I/home/youka/Schreibtisch/Projekte/FLuaG/build/src -I/usr/include/luajit-2.0 -I/usr/local/include -Wall -Wextra -pedantic -Wredundant-decls -Wunreachable-code -Wmissing-include-dirs -Wswitch-enum -Wno-missing-field-initializers -std=c++11 -mmmx -o CMakeFiles/fluag.dir/main/FLuaG.cpp.o -c /home/youka/Schreibtisch/Projekte/FLuaG/src/main/FLuaG.cpp
Link command which causes error:
/usr/bin/c++ -fPIC -g -shared -Wl,-soname,libfluag.so -o libfluag.so CMakeFiles/fluag.dir/lualibs/utf8.cpp.o CMakeFiles/fluag.dir/lualibs/mathx.cpp.o CMakeFiles/fluag.dir/lualibs/regex.cpp.o CMakeFiles/fluag.dir/lualibs/png.cpp.o CMakeFiles/fluag.dir/lualibs/geometry.cpp.o CMakeFiles/fluag.dir/lualibs/tablex.cpp.o CMakeFiles/fluag.dir/lualibs/filesystem.cpp.o CMakeFiles/fluag.dir/lualibs/tgl.cpp.o CMakeFiles/fluag.dir/lualibs/font.cpp.o CMakeFiles/fluag.dir/interfaces/vapoursynth.cpp.o CMakeFiles/fluag.dir/interfaces/public.cpp.o CMakeFiles/fluag.dir/main/FLuaG_image.cpp.o CMakeFiles/fluag.dir/main/FLuaG.cpp.o -Wl,-Bstatic -lluajit-5.1 -Wl,-Bdynamic -lGLEW /usr/local/lib/libglfw3.a -lGL -lpng -lz
Compiling this project with MinGW64 on Windows Vista works. Release or Debug build, with or without SIMD, etc. makes no difference.
The error message tells me that in file wcsrtombs.c (linked libgcc), line 99, memory comparison data.__outbuf[-1] == '\0' isn't ensured... which pretty confuses me.
After setting the system language to english, the error message suddenly made sense:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libluajit-5.1.a(ljamalg.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libluajit-5.1.a: error adding symbols: Bad value
The problem was -Wl,-Bstatic -lluajit-5.1. I linked to the .a file which was expected to be a static library by CMake. Changing to .so solved the problem.
#include <iostream>
#include <Magick++.h>
int main()
{
Magick::InitializeMagick(NULL);
Magick::Image im;
im.read("/home/chase/Desktop/m42.jpg");
im.display();
return 0;
}
I get the following error when I try to compile in eclipse...
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/include/x86_64-linux-gnu/ImageMagick-6 -I/usr/include/ImageMagick-6 -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: Stacking
Invoking: GCC C++ Linker
g++ -L../ -o "Stacking" ./main.o -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16 -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16
./main.o: In function `main':
/home/chase/workspace/Stacking/Debug/../main.cpp:8: undefined reference to `Magick::Image::read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
makefile:44: recipe for target 'Stacking' failed
make: *** [Stacking] Error 1
Why does the read function not compile. I used the package config tool in eclipse to set up Magick++. Also as far as I can tell all the other functions work fine. I am using Ubuntu. I installed Magic++ using sudo apt-get install libmagick++-dev.
Update:
I got it to work. I had upgraded to g++-5. When I compiled with g++-4.9 it worked. Wonder why it does not work with g++-5?
Why does the read function not compile
Your read function does compile. What you have is a link problem.
why it does not work with g++-5
Because g++-4.x and g++-5.x use different ABI, and are not link-compatible (and your libMagick* libraries were built with g++-4.x).
I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.
after trying to cross compile a program for a week i gave up and now im trying to compile it directly on the friendlyarm running qtopia 2.2.0, but when i run make i am getting some strange errors. Can some one shine some more light on the and maybe point me in the right direction on how to fix the problem?
the make output:
/sdcard/images/makef # make
make: Warning: File `Makefile' has modification time 2.2e+04 s in the future
gcc -c -o obj/main.o main.c -I./
gcc -c -o obj/serial.o serial.c -I./
gcc -c -o obj/fb.o fb.c -I./
gcc -c -o obj/menu_main.o menu_main.c -I./
gcc -c -o obj/timer.o timer.c -I./
gcc -c -o obj/cmdin.o cmdin.c -I./
cmdin.c: In function 'processcmd':
cmdin.c:64: warning: format '%f' expects type 'float *', but argument 4 has type 'int *'
gcc -c -o obj/buzzer.o buzzer.c -I./
gcc -c -o obj/statemachine.o statemachine.c -I./
gcc -c -o obj/inout.o inout.c -I./
gcc -c -o obj/network.o network.c -I./
gcc -c -o obj/text_file_input.o text_file_input.c -I./
gcc -c -o obj/text_file_input_oven.o text_file_input_oven.c -I./
gcc -o main obj/main.o obj/serial.o obj/fb.o obj/menu_main.o obj/timer.o obj/cmdin.o obj/buzzer.o obj/statemachine.o obj/inout.o obj/network.o obj/text_file_input.o obj/text_file_input_oven.o -I./ -lgd -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.a when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
/usr/bin/ld: skipping incompatible /usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.a when searching for -lrt
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_create.o): In function `timer_create':
timer_create.c:(.text+0xd4): undefined reference to `pthread_once'
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o): In function `timer_helper_thread':
timer_routines.c:(.text+0xfc): undefined reference to `pthread_create'
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o): In function `__start_helper_thread':
timer_routines.c:(.text+0x1a0): undefined reference to `pthread_attr_setstacksize'
timer_routines.c:(.text+0x1e4): undefined reference to `pthread_create'
timer_routines.c:(.text+0x228): undefined reference to `pthread_atfork'
collect2: ld returned 1 exit status
make: *** [main] Error 1
/sdcard/images/makef #
Also how can i get if this message:
make: Warning: File `Makefile' has modification time 2.2e+04 s in the future
i tried
touch *.*
but that didn't help
make: Warning: File `Makefile' has modification time 2.2e+04 s in the
future
Your clocks on the system you are compiling on and the system that generated the files are out of sync. You should fix that (in another answer is mentioned a workaround using touch, but this is only a good idea if the "other computer" is running the wrong time - if your system you are compiling on is running the wrong time, then you should fix the time on the current system - ideally using ntp (network time protocol) to set your time on both systems from a network source at boot or similar - that way, you don't have to worry about them going out of phase either [a PC system clock will drift between 1 and 30 seconds in a month, depending on the actual hardware used].
/usr/bin/ld: skipping incompatible
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/librt.so when searching for -lrt
These messages are harmless as long as the system is able to find some librt that "is compatible", and it seems like it does, since we get this.
/usr/lib/gcc/arm-linux-gnueabi/4.4.1/../../../librt.a(timer_routines.o)
The following indicates that libpthread is not being linked (in the right place)
timer_create.c:(.text+0xd4): undefined reference to `pthread_once'
You need to use -lpthread on your linker line - after -lrt, since librt is what is using pthread functions. Note that libraries are sensitive to order (and sometimes you even need to give the same library twice, because there are cyclic dependencies)
That message is usually an indication that some of your files have modification times later than the current system time. A possible solution is to "touch" every file in the source tree in order to update timestamps: go to the root of the subtree an do "find . -exec touch {} \; "
Then clean your project, delete all build files and retry compilation.
It seems that You have also forgotten to link libpthread. You should have posix libraries and link your project to libpthread.
I'm trying to embed python code in C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev).
So, this is the simple code:
#include <Python.h> //Python.h
#include <iostream> //iostream
using namespace std;
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
return 0;
}
And I couldn't understant what am I doing wrong.
I include dirrctories C:\Python27\include and C:\Python27\libs but I can't build my project.
1) When I trying to build my project I got this error:
**** Internal Builder is used for build **** g++
-IC:\Python27\include -IC:\Python27\libs -O0 -g3 -Wall -c
-fmessage-length=0 -o main.o ..\main.cpp g++ -o testpy2.exe main.o
main.o: In function `main':
C:\Users\const\workspace\testpy2\Debug/../main.cpp:7: undefined
reference to `_imp__Py_Initialize'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:9: undefined
reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:10: undefined
reference to `_imp__Py_Finalize'
collect2: ld returned 1 exit status
Build error occurred, build is stopped Time consumed: 1507 ms.
2) And if I change current toolchain in Eclipse from "minGW" to "CrossGCC" .. I got this error:
**** Build of configuration Release for project testpy ****
make all Building file: ../main.cpp Invoking: Cross G++ Compiler g++
-I"C:\Python27\include" -I"C:\Python27\libs" -O3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o"
"../main.cpp" Finished building: ../main.cpp Building target:
testpy.exe Invoking: Cross G++ Linker g++ -o "testpy.exe" ./main.o
-l"C:/Python27/libs/libpython27.a" -l"C:/Python27/libs/python27.lib"
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/libpython27.a
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/python27.lib collect2: ld returned 1
exit status make: *** [testpy.exe] Error 1
**** Build Finished ****
Could anybody tell me what's wrong with my code or settings or something else?
Thank you
That is a linker error, not a compiler error. You need to link to the python. As you can see, with the "CrossGCC" toolchain you are almost there:
-lC:/Python27/libs/libpython27.a
You need to change this to
-LC:/Python27/libs -lpython