Undefined reference to SDL_init, build log unclear - sdl

I have just setup SDL on CodeBlocks, and I am getting a build error:
-------------- Build: Debug in SDL learning (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -L..\deps\lib -o "bin\Debug\SDL learning.exe" obj\Debug\main.o -lmingw32 -lSDL2main -lSDL2
obj\Debug\main.o: In function `SDL_main':
C:/Users/73638G75MA/Documents/prive/programeren/C++ projects/SDL learning/main.cpp:7: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
The build log doesn't really tell me anything, maybe some of you guys know a solution.
In case you need my code:
#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char *argv[]){
if(SDL_Init(SDL_INIT_EVERYTHING)!=0){
cout << "Could not initiate SDL" << endl;
}
return 0;
}
I got my include and lib files in folder named deps.
Also, it does recognize the SDL.h file and its commands, because when I type #include <S it suggests SDL.h. It also autocompletes the SDL commands.

Related

Undefined reference to `mysql_init' , Undefined reference to `mysql_real_connect using MYSQL C++ in codeblock

I added mysql.a in Project->Build Options->Linker Settings->path to mysql.a
I added "include files containing mysql.h" in Project->Build Options->Linker Settings->path to incldue files.
I also added mysql.dll files in debug folder of the project directory.
But still I am getting this error:
||=== Build: Debug in wss4 (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
D:\C++Ws\ws4\wss4\main.cpp|11|undefined reference to `mysql_init'|
D:\C++Ws\ws4\wss4\main.cpp|12|undefined reference to `mysql_real_connect'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I am using codeblock 17.12, also tried codeblock 20(latest version) but still getting the same error.
Below is my small code snippet:
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL* conn;
conn=mysql_init(0);
conn=mysql_real_connect(conn,"localhost","admin","admin","youtube",0,NULL,0);
if(conn){
cout<<"Connected";
}
else{
cout<<"Not connected";
}
}
Please any one show the problem solving steps. It has been taking hours and hours. Thanks

Codeblocks doesn't support glfw3?

I'm learning opengl and wanted to use Codeblocks IDE because visual studio seems too laggy and slow. After starting a console application project i've tried to link GLFW3 library to make this code run:
#include <GLFW/glfw3.h>
#include <thread>
int main()
{
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
}
I use GNU GCC 32-bit Compiler from MinGW(one that comes with codeblocks installation). I'm pretty certain that i've done everything correctly, i've read through like every article that i was able to find explaining how to setup codeblocks for opengl. I've linked libglfw3.a and libglfw3dll.a and set up correct search directories for compiler and linker but i always get this undefined reference error no matter what:
-------------- Build: Debug in glfw_again_test (compiler: GNU GCC Compiler)---------------
g++.exe -L"C:\Program Files\CodeBlocks\MinGW\lib" -o bin\Debug\glfw_again_test.exe obj\Debug\main.o "C:\Program Files\CodeBlocks\MinGW\lib\libglfw3.a" "C:\Program Files\CodeBlocks\MinGW\lib\libglfw3dll.a" "C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\lib\libopengl32.a"
obj\Debug\main.o: In function `main':
C:/dev/OpenGL/CodeBlocks/glfw_again_test/main.cpp:6: undefined reference to `glfwInit'
C:/dev/OpenGL/CodeBlocks/glfw_again_test/main.cpp:8: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
3 error(s), 0 warning(s) (0 minute(s), 1 second(s))
I have no idea what else i could try to fix this problem.
Apparently i was mistaken and thought that i use 32-bit MinGW compiler but seems like i accidentally installed 64-bit one with codeblocks. After switching to mingw gcc 32-bit compiler and rebuilding(simple build will lead to an error) my project i finally got it to work(not exactly this code snippet but another sample one).

"Undefined reference" errors when including <iostream>

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.

C++ can't compile when I use iostream (Linux, CodeBlocks)

I want to use C++ Code in Java. I know I have to use JNI. And now I want to compile a shared library in C++ on Linux. I using the Code::Blocks 16.01 IDE and the GNU GCC Compiler.
And now I want to compile this very simple code:
#include <iostream>
void sayHello ()
{
std::cout << "Hello from C++" << std::endl;
}
(I know that I also have to include some more to use this library later in java, but this was just my first test, if it compiles)
The Build log is:
g++ -shared obj/Debug/main.o -o bin/Debug/libnativetest.so
/usr/bin/ld: obj/Debug/main.o: Die Umlagerung von
obj/Debug/main.o: error adding symbols: Ungültiger Wert
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
The Build messages:
||error: ld returned 1 exit status|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
When I just compile the method without#include <iostream> (and sure without cout) it works.
I have tested with another header file like #include <string> and this works.
When I create a project not as a shared library instead a Console Application I can use <iostream>.
I am new to C++ and Code::Blocks therefore if you need some more info please help me where I can find this information.
I do not use Code blocks, so I have no idea how it decided to leave out the fPIC flag. If you try to build it on the command line with
g++ main.cpp -o main.o
The gcc (v5.4 in my case) prompts you to add -fPIC flag, with with the error disappears and the compilation succeeds. fPIC stands for position independent code. i.e., the function addresses (jump addresses) are not hard coded but are left blank. The loader, when it needs the .so, loads it to the memory and then fills the jump addresses dynamically. (-fpic could also work, it is for short jumps, whereas fPIC accommodates also long jumps, i.e., bigger jump ranges at the cost of binary size.)

undefined references SDL with Code::Blocks [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I was following http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php tutorial on how to use SDL with Code::Blocks since I've been having trouble with this in pretty much every damn IDE I've tried.
The tutorial is pretty straight forward, on step number 7 it states
"we have to tell the compiler to link against the libraries. Go under Linker Settings and paste -lmingw32 -lSDL2main -lSDL2". I did exactly that. Then it says that if you get a bunch of undefined reference errors, you messed up this step, I don't really see how it is possible for me to mess up this step, since it is a simple step.
I would really like to get started with this, while using MinGW and Code::Blocks.
Information that might help resolve this:
I have MinGW directory located in my C:
I have a folder SDL in my C: directory, within that folder I am linking the include and lib files from SDL to Code::BLocks by right clicking on project properties and adding the directories. This all seems to be working fine.Include Directory = C:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 Lib Directory = C:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\lib
As stated above, on the Linker Setting -> Other Linker Options: I wrote -lmingw32 -lSDL2main -lSDL2, yet I get a bunch of reference errors. I don't know what to try, I have been searching online for hours, I even replaced SDL_platform.h because it was causing issues and the undefined references are still there.
Please help. This is the code I am using to check if SDL is working, it isn't.
#include "SDL.h"
#include <iostream>
#include <cstdio>
#include <Windows.h>
int main( int argc, char* argv[])
{
// Fire up SDL, this starts all subsystems; audio video etc.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Now Shut it down
atexit(SDL_Quit);
return 0;
}
These are the errors I am getting:
-------------- Build: Debug in TITLE (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -IC:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -c C:\Users\Bryan\Desktop\CodeBlocks\TITLE\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LC:\SDL\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\TITLE.exe obj\Debug\main.o -lmingw32 -lSDL2main -lSDL2
obj\Debug\main.o: In function `SDL_main':
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:10: undefined reference to `SDL_Init'
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:11: undefined reference to `SDL_GetError'
C:/Users/Bryan/Desktop/CodeBlocks/TITLE/main.cpp:15: undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 0 warning(s) (0 minute(s), 0 second(s))
You have created a Windows executable project. The wizard set the entry point to WinMain. Your code implements a command line program with main(int argc, char**argv) as entry point.
If you want to stay with the main you should create a new command line project and add you
source files to this project. Alternativly you could try to change the project type.
For the SDL errors you should check, that you use matching compiler and libraries (32 vs. 64 bit).