"Undefined reference" errors when including <iostream> - c++

I have started a new console application (C++) in Code::Blocks 17.12 with Cygwin compiler and g++ 7.3.0, and I have only "main.cpp" which have a very small code:
#include<iostream>
using namespace std;
int main(){
cout<<"test";
return 0;
}
I clicked build and got 16 errors, Here's the build log:
-------------- Build: Debug in 1 (compiler: Cygwin g++)---------------
g++.exe -Wall -fexceptions -g -std=c++14 -IC:\cygwin\usr\include -c C:\Users\Windows7\Desktop\BASIL\1\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LC:\cygwin\lib -o bin\Debug\1.exe obj\Debug\main.o
obj\Debug\main.o: In function `_static_initialization_and_destruction_0':
/usr/lib/gcc/i686-pc-cygwin/7.3.0/include/c++/iostream:74: undefined reference to `__dso_handle'
/usr/lib/gcc/i686-pc-cygwin/7.3.0/include/c++/iostream:74: undefined reference to `__cxa_atexit'
C:\cygwin\lib/libpthread.a(t-d001702.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_destroy'
C:\cygwin\lib/libpthread.a(t-d001704.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_init'
C:\cygwin\lib/libpthread.a(t-d001709.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_unlock'
C:\cygwin\lib/libpthread.a(t-d001720.o):fake:(.text+0x2): undefined reference to `_imp__pthread_once'
C:\cygwin\lib/libpthread.a(t-d001705.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_lock'
C:\cygwin\lib/libpthread.a(t-d001699.o):fake:(.text+0x2): undefined reference to `_imp__pthread_key_create'
C:\cygwin\lib/libpthread.a(t-d001697.o):fake:(.text+0x2): undefined reference to `_imp__pthread_getspecific'
C:\cygwin\lib/libpthread.a(t-d001741.o):fake:(.text+0x2): undefined reference to `_imp__pthread_setspecific'
C:\cygwin\lib/libpthread.a(t-d001679.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_wait'
C:\cygwin\lib/libpthread.a(t-d001674.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_broadcast'
C:\cygwin\lib/libpthread.a(t-d001700.o):fake:(.text+0x2): undefined reference to `_imp__pthread_key_delete'
C:\cygwin\lib/libpthread.a(t-d001675.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_destroy'
C:\cygwin\lib/libpthread.a(t-d001677.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_signal'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 3 second(s))
16 error(s), 0 warning(s) (0 minute(s), 3 second(s))
I also edited the compiler, the linker and the resource compiler in search directories tab according to the documentation but that didn't work.
please help me in this!
Thanks,

Finally fixed it! Just giving the answer to people still searching,
just install Cygwin and go to settings -> compiler in code::blocks and pick Cygwin GCC instead of GNU GCC compiler, go to the Toolchain excutables and change the "Compiler's Installation Directory" to your Cygwin directory and change the "Program files" to your cygwin programs (if they're not already modified), the compiler window should look like the screenshot attached.

Related

Issue with compiling SFML project in CodeBlocks

Made a simple sprite based 2d game in c++ using SFML. It compiles and runs just fine on linux but I want to be able to compile it on windows
I've downloaded the (supposed) right version of sfml for code::blocks this one from https://www.sfml-dev.org/download/sfml/2.5.1/ and configured the GNU GCC compiler search paths accordingly:
compiler search paths
linker search paths
c++11 standard enabled
After running the code I got 152 "undefined reference to ..." errors
Then after adding the libraries to the linker settings
linker settings
I got left with those 12 errors:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf5Music12openFromFileERKSs'| C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf5Music12openFromFileERKSs'|
C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf5Music12openFromFileERKSs'| C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf5Music12openFromFileERKSs'|
C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf4Font12loadFromFileERKSs'| C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE'|
C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE'| C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE'|
C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE'| C:\sfmltest\main.o:main.cpp|| undefined reference to _imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE'|
C:\sfmltest\main.o:main.cpp|| more undefined references to `_imp___ZN2sf7Texture12loadFromFileERKSsRKNS_4RectIiEE' follow|
||error: ld returned 1 exit status|
||=== Build failed: 12 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
what am I missing?
You didn't define SFML_STATIC which is why the linker is looking for _imp___ (i.e. import) symbols.
Also your listed libraries is wrong. You should only list one set of libraries.
For example Static and Debug = sfml-*-s-d
Or Static and Release = sfml-*-s
And when you're statically linking, you also need to link SFML's dependencies.
If you follow the official tutorial on how to setup SFML with Code::Blocks step by step and don't just looking at the images, things should work just fine.

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.

Error: undefined reference to '...' after trying to build app in fresh system installation

I am developing c++ application in Win7 OS using GCC compiler and Code::Blocks as IDE. I have everything set up and everything works. Now i need to move whole development environment to virtual machine so i have installed system, IDE compiler, copied whole project. I double checked all libraries and options and they are same. When i try to build application i get this errors (50) from build log(relevant parts):
-------------- Build: Debug in MagicReportsFull (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -std=c++11 -fexceptions -std=c++11 -g -IC:\Projects\MagicReportsFull\include -IC:\Projects\MagicReportsFull\ -I"C:\Program Files (x86)\CodeBlocks\MinGW" -IC:\Projects\MagicReportsFull -I"C:\Program Files (x86)\CodeBlocks\MinGW" -IC:\Projects\MagicReportsFull\include -c C:\Projects\MagicReportsFull\main.cpp -o obj\Debug\main.o
windres.exe -J rc -O coff -i C:\Projects\MAGICR~1\resource.rc -o obj\Debug\resource.res
windres.exe -J rc -O coff -i C:\Projects\MAGICR~1\version.rc -o obj\Debug\version.res
mingw32-g++.exe -LC:\Projects\MagicReportsFull\ -o bin\Debug\MagicReportsFull.exe obj\Debug\main.o obj\Debug\resource.res obj\Debug\version.res -lcomdlg32 -lgdi32 -luser32 -lshlwapi ..\..\CustomLibraries\lpng1636-build\libpng16.a ..\..\CustomLibraries\zlib-1.2.11-build\libzlibstatic.a ..\..\CustomLibraries\libharu-RELEASE_2_3_0-build\src\libhpdfs.a -lcomctl32 -mwindows
.
.
.
obj\Debug\main.o: In function `Z19CUSTOM_RegularTableP13_HPDF_Doc_RecP14_HPDF_Dict_RecffPfiS3_ic':
C:/Projects/MagicReportsFull/pdf.h:99: undefined reference to `HPDF_Page_Rectangle'
C:/Projects/MagicReportsFull/pdf.h:100: undefined reference to `HPDF_Page_Stroke'
C:/Projects/MagicReportsFull/pdf.h:102: undefined reference to `HPDF_Page_Rectangle'
C:/Projects/MagicReportsFull/pdf.h:103: undefined reference to `HPDF_Page_Stroke'
C:/Projects/MagicReportsFull/pdf.h:114: undefined reference to `HPDF_Page_MoveTo'
C:/Projects/MagicReportsFull/pdf.h:115: undefined reference to `HPDF_Page_LineTo'
C:/Projects/MagicReportsFull/pdf.h:116: undefined reference to `HPDF_Page_Stroke'
C:/Projects/MagicReportsFull/pdf.h:125: undefined reference to `HPDF_Page_MoveTo'
C:/Projects/MagicReportsFull/pdf.h:126: undefined reference to `HPDF_Page_LineTo'
C:/Projects/MagicReportsFull/pdf.h:127: undefined reference to `HPDF_Page_Stroke'
obj\Debug\main.o: In function `Z27CUSTOM_RegularTable_AddText20_CUSTOM_TableHandleriiPKhjccP14_HPDF_Dict_Recj':
C:/Projects/MagicReportsFull/pdf.h:198: undefined reference to `HPDF_Font_GetCapHeight'
C:/Projects/MagicReportsFull/pdf.h:232: undefined reference to `HPDF_Font_MeasureText'
C:/Projects/MagicReportsFull/pdf.h:294: undefined reference to `HPDF_Page_SetFontAndSize'
C:/Projects/MagicReportsFull/pdf.h:300: undefined reference to `HPDF_Font_MeasureText'
C:/Projects/MagicReportsFull/pdf.h:315: undefined reference to `HPDF_Page_BeginText'
C:/Projects/MagicReportsFull/pdf.h:316: undefined reference to `HPDF_Page_TextRect'
C:/Projects/MagicReportsFull/pdf.h:317: undefined reference to `HPDF_Page_EndText'
obj\Debug\main.o: In function `Z13pdf_generatorcPci':
C:/Projects/MagicReportsFull/pdf.h:347: undefined reference to `HPDF_New'
C:/Projects/MagicReportsFull/pdf.h:353: undefined reference to `HPDF_SetCompressionMode'
C:/Projects/MagicReportsFull/pdf.h:355: undefined reference to `HPDF_GetFont'
C:/Projects/MagicReportsFull/pdf.h:356: undefined reference to `HPDF_GetFont'
C:/Projects/MagicReportsFull/pdf.h:357: undefined reference to `HPDF_GetFont'
C:/Projects/MagicReportsFull/pdf.h:358: undefined reference to `HPDF_GetFont'
C:/Projects/MagicReportsFull/pdf.h:362: undefined reference to `HPDF_LoadPngImageFromFile'
C:/Projects/MagicReportsFull/pdf.h:442: undefined reference to `HPDF_AddPage'
C:/Projects/MagicReportsFull/pdf.h:444: undefined reference to `HPDF_Page_SetSize'
C:/Projects/MagicReportsFull/pdf.h:446: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:447: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:460: undefined reference to `HPDF_Page_DrawImage'
C:/Projects/MagicReportsFull/pdf.h:463: undefined reference to `HPDF_Page_BeginText'
C:/Projects/MagicReportsFull/pdf.h:464: undefined reference to `HPDF_Page_SetRGBFill'
C:/Projects/MagicReportsFull/pdf.h:465: undefined reference to `HPDF_Page_SetFontAndSize'
C:/Projects/MagicReportsFull/pdf.h:466: undefined reference to `HPDF_Page_TextRect'
C:/Projects/MagicReportsFull/pdf.h:467: undefined reference to `HPDF_Page_EndText'
C:/Projects/MagicReportsFull/pdf.h:685: undefined reference to `HPDF_AddPage'
C:/Projects/MagicReportsFull/pdf.h:687: undefined reference to `HPDF_Page_SetSize'
C:/Projects/MagicReportsFull/pdf.h:689: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:690: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:699: undefined reference to `HPDF_Page_DrawImage'
C:/Projects/MagicReportsFull/pdf.h:702: undefined reference to `HPDF_Page_BeginText'
C:/Projects/MagicReportsFull/pdf.h:703: undefined reference to `HPDF_Page_SetRGBFill'
C:/Projects/MagicReportsFull/pdf.h:704: undefined reference to `HPDF_Page_SetFontAndSize'
C:/Projects/MagicReportsFull/pdf.h:705: undefined reference to `HPDF_Page_TextRect'
C:/Projects/MagicReportsFull/pdf.h:706: undefined reference to `HPDF_Page_EndText'
C:/Projects/MagicReportsFull/pdf.h:829: undefined reference to `HPDF_AddPage'
C:/Projects/MagicReportsFull/pdf.h:831: undefined reference to `HPDF_Page_SetSize'
C:/Projects/MagicReportsFull/pdf.h:833: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:834: undefined reference to `HPDF_Page_SetLineWidth'
C:/Projects/MagicReportsFull/pdf.h:843: undefined reference to `HPDF_Page_DrawImage'
C:/Projects/MagicReportsFull/pdf.h:846: undefined reference to `HPDF_Page_BeginText'
Process terminated with status 1 (0 minute(s), 5 second(s))
50 error(s), 37 warning(s) (0 minute(s), 5 second(s))
As you can see, I am using LibHaru library which works flawlesly in guest system. Also i believe problem is not in library itself but in linker.
Project build options -> Linker settings -> Link libraries:
libcomdlg32.a
libgdi32.a
libuser32.a
libshlwapi.a
..\..\CustomLibraries\lpng1636-build\libpng16.a
..\..\CustomLibraries\zlib-1.2.11-build\libzlibstatic.a
..\..\CustomLibraries\libharu-RELEASE_2_3_0-build\src\libhpdfs.a
libcomctl32.a
I tried to do the same thing in Win7 virtual machine with same results. I am using same files as in guest system. Any ideas what went wrong?
I posted this question because i was not sure about linker settings being ok but since nobody said that its wrong i searched for other problems. So it turns out that i have been using wrong Program Files in Global compiler settings -> Toolchain executables. Also library order was wrong, LibHaru needed to be first and then other dependent libraries. Uninstalled GCC and IDE then installed correct version of GCC + IDE. Now it compiles without problem.
I suspect that reinstalling the compiler might have changed the version of MinGW, and maybe the library isn't compatible between these 2 versions.
I've had similar weird behaviour with MinGW before, and recompiling the dependencies myself with the same compiler fixed the issue (it's a tedious solution though).
Are the compiler and system exactly the same ? (same architecture, version, etc)
Check if you havent copied some codeblocks environment specific files from the previous system, maybe codeblocks is trying to compile with options that doesn't apply to your new environment.
Does the libhpdfs headers have system dependent defines ?
Did you compile libhpdfs.a yourself ?
Did you try recompiling it yourself on the virtual machine ?

Static linking OpenSSL and cURL with Autotools and MinGW

I'm trying to build a statically linked OpenSSL + LibCurl console application with CodeBlocks. However, I can't get it to work. I keep getting these errors:
||=== Build: Debug in Filer (compiler: GNU GCC Compiler) ===|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_get0_alpn_selected'|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_CTX_set_alpn_protos'|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_CTX_set_next_proto_select_cb'|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_CTX_set_srp_username'|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_CTX_set_srp_password'|
C:\MinGW\msys\1.0\local\lib\libcurl.a(libcurl_la-openssl.o)|| undefined reference to `SSL_COMP_free_compression_methods'|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
This is the build log:
mingw32-g++.exe -o bin\Debug\Filer.exe "obj\Debug\MinGW Sources\BASE64\base64.o
" "obj\Debug\MinGW Sources\SQLite\sqlite3.o" obj\Debug\Users\thoma\Desktop\Filer
\main.o -static -lcurl -lrtmp -lidn -lssl -lssh2 -lcrypto -lz -lwldap32 -lws2_
32 -lwinmm -lgdi32 C:\MinGW\msys\1.0\local\lib\libcrypto.dll.a C:\MinGW\msys\1.0
\local\lib\libcurl.a C:\MinGW\msys\1.0\local\lib\libcurl.dll.a C:\MinGW\msys\1.0
\local\lib\libssl.a C:\MinGW\msys\1.0\local\lib\libssl.dll.a C:\MinGW\msys\1.0\l
ocal\lib\libwxjpeg-3.0.a C:\MinGW\msys\1.0\local\lib\libwxpng-3.0.a C:\MinGW\msy
s\1.0\local\lib\libwxscintilla-3.0.a C:\MinGW\msys\1.0\local\lib\libwxtiff-3.0.a
C:\MinGW\msys\1.0\local\lib\libz.a C:\MinGW\msys\1.0\local\lib\libz.dll.a
I have used the set-up which is outlined here.
Some pictures of my setup:
You have a mismatch between your OpenSSL version and the OpenSSL version used by libcurl.

with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

I am new to posix thread library, and I tried to compile a sample code from a tutorial with:
g++ -lpthread agreement.cpp -o agreement
however I was not able to compile the code and got the following error message:
a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1
I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the "proper" pthread library?
Thanks for your help!
You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt