Errors while using libgraph library for "graphics.h" - c++

I tried to use libgraph library on ubuntu to use "graphics.h" for a sample program .
But while compiling i get these errors :
g++ graph.cpp -o graph -lgraph
/tmp/ccD6rrqg.o: In function `main':
graph.cpp:(.text+0x32): undefined reference to `initgraph'
graph.cpp:(.text+0x37): undefined reference to `cleardevice'
graph.cpp:(.text+0xf8): undefined reference to `grgetch'
graph.cpp:(.text+0xfd): undefined reference to `closegraph'
/tmp/ccD6rrqg.o: In function `bresen(int, int, int, int)':
graph.cpp:(.text+0x1f9): undefined reference to `putpixel'
graph.cpp:(.text+0x206): undefined reference to `delay'
collect2: ld returned 1 exit status
Do I need to add any more linker flags ? This program works on windows so I think the program itself is right .
I managed to get it run but the window crashes giving :
process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed.
also can I resize the window to full screen ?? thankx!

The installation instructions for libgraph seem a bit... casual. Chances are the library should just be explicitly linked in:
g++ graph.cpp -o graph /path/to/libgraph.a
or wherever it was installed.
Another possibility to investigate what library g++ uses:
g++ -print-file-name=graph
though on my system, it just echoes graph.

Related

Undefined Reference to `SDL2_Init` with LD when G++ is not giving an error

(FYI Yes I do know that a similar question exists, But I have already tried all the answers.)
I'm trying to setup SDL2 with MinGW and I have everything set up beside one thing. Whenever I try to compile G++ gives no errors but then LD gives me a: undefined reference to SDL_Init Error.
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\cc28dE2R.o: in function `SDL_main':
C:\Users\user\Documents\SDL2/src/main.cpp:11: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:14: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:18: undefined reference to `IMG_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:20: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lmingw32 -lSDL2main -lSDL2_image -lSDL2" terminated with exit code: 1.
I have tried to move the args around. doing -mwindows, adding -lmingw32. I still keep getting the same errors.. I was able to fix the WinMain Error.. But everything else I couldn't fix.
The command I'm Running to compile:
C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lSDL2main -lSDL2_image -lSDL2
If i could get any help that would be amazing
The common cause of mysterious undefined reference errors on MinGW is using libraries compiled for x64 with an x32 compiler, or vice versa. SDL ships both x32 and x64 ones, try the other ones.
I was able to fix the WinMain Error
Be aware that the intended way of fixing undefined reference to WinMain#16 is not adding #define SDL_MAIN_HANDLED and not doing #undef main. Once you get the right libraries (see the first paragraph), the error should disappear by itself.
Make sure you use int main(int, char**) (and not int main() nor void main()), otherwise it won't work.
-ID:C:\MinGW\include -LD:C:\MinGW\lib
You shouldn't need those flags. Those directories will be searched automatically.
Since you didn't specify any other directories, I assume you placed the SDL files directly into the compiler directories, which isn't a good practice, IMO.
I assume D:C: a typo?
-mwindows
The only purpose of this flag (that I know of) is to prevent your app from automatically opening a terminal window for itself, for release builds.

How to link/include c++ libraries correctly

I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.

Undefined reference to nlopt_... symbols

I am fairly new to Fortran and this may sound like a silly question. I encounter an error when compiling the Fortran code that is posted as an example in the NLOPT Wiki.
I compile in Ubuntu 18.04 LTS using the following command:
gfortran example-nlopt.f90 -o example-nlopt -I/usr/local/include/
The following error is produced in the terminal:
/tmp/ccbAim6b.o: In function `MAIN__':
example-nlopt.f90:(.text+0x26): undefined reference to `nlo_create_'
example-nlopt.f90:(.text+0x42): undefined reference to `nlo_get_lower_bounds_'
example-nlopt.f90:(.text+0x67): undefined reference to `nlo_set_lower_bounds_'
example-nlopt.f90:(.text+0x8a): undefined reference to `nlo_set_min_objective_'
example-nlopt.f90:(.text+0xca): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x10e): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x12d): undefined reference to `nlo_set_xtol_rel_'
example-nlopt.f90:(.text+0x164): undefined reference to `nlo_optimize_'
example-nlopt.f90:(.text+0x305): undefined reference to `nlo_destroy_'
collect2: error: ld returned 1 exit status
Based on what I saw in nlopt's documentation (https://nlopt.readthedocs.io/en/latest/NLopt_Installation/#changing-the-installation-directory) it looks like you just need to specify the library to link against. Maybe try this:
gfortran -I/usr/local/include/ -L/usr/local/lib example-nlopt.f90 -o example-nlopt -lnlopt -lm
This assumes you have the libnlopt.so in /usr/local/lib, if not then point to its location with the -L option.

Undefined reference to Magick::Image ::Image() in eclipse c++

I am using Eclipse CDT on windows 7 64 bit and trying to work on Image Class using Image Magick but I am getting the below error-
#pragma warning(once: 4459) /* 'identifier' : declaration of 'foo' hides global declaration */
g++ "-LC:\\Development\\ImageMagick-7.0.3-Q16\\lib" "- LC:\\Development\\opencv\\MINGW\\x86\\bin" -o testOpencv.exe "src\\testOpencv.o" -llibopencv_features2d2410 -lCORE_RL_Magick++_ -lCORE_RL_MagickCore_ - lCORE_RL_MagickWand_ -llibopencv_objdetect2410 -llibopencv_contrib2410 - llibopencv_core2410 -llibopencv_flann2410 -llibopencv_gpu2410 - llibopencv_highgui2410 -llibopencv_imgproc2410 -llibopencv_ml2410 - llibopencv_legacy2410 -llibopencv_nonfree2410 -llibopencv_objdetect2410 - llibopencv_ocl2410 -llibopencv_photo2410 -llibopencv_stitching2410 - llibopencv_superres2410 -llibopencv_video2410 -llibopencv_videostab2410 - lopencv_ffmpeg2410 -llibopencv_ml2410
src\testOpencv.o: In function `main':
C:\Development\Workspace\testOpencv\Debug/../src/testOpencv.cpp:14: undefined reference to `Magick::Image::Image()'
C:\Development\Workspace\testOpencv\Debug/../src/testOpencv.cpp:15: undefined reference to `Magick::Image::read(std::string const&)'
C:\Development\Workspace\testOpencv\Debug/../src/testOpencv.cpp:16: undefined reference to `Magick::Image::display()'
C:\Development\Workspace\testOpencv\Debug/../src/testOpencv.cpp:16: undefined reference to `Magick::Image::~Image()'
C:\Development\Workspace\testOpencv\Debug/../src/testOpencv.cpp:16: undefined reference to `Magick::Image::~Image()'
collect2.exe: error: ld returned 1 exit status
14:28:39 Build Finished (took 847ms)
I have added includes file to compiler and Library files in the linker in Eclipse. The library file in Image magick installed folder look strange to me, as they are not '.dll' files. Please let me know if I am right and how do i rectify this.

__imp link errors using g++ running under mingw

I have a simple socket program that I'm trying to compile using g++ running in mingw (both the latest versions) on a Win8 system. I'm getting the common linker errors
undefined reference to `__imp_socket'
undefined reference to `__imp_gethostbyname'
I've tried adding -lws2_32 with no luck; i.e. it still can't find the references. Can someone suggest something else I might be missing?
Here's the full output:
G:\source\kak>g++ -o ./test_client -lws2_32 test_client.C
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x4f): undefined reference to `__imp_inet_addr'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x6b): undefined reference to `__imp_socket'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x8b): undefined reference to `__imp_connect'
d:/program files/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o: bad reloc address 0xc in section `.xdata'
collect2.exe: error: ld returned 1 exit status
Try putting the -lws2_32 after the test_client.C parameter. The linker of gcc (ld) is touchy about the order of linkable things, this is probably why it doesn't find your imported functions at link time.