undefined references SDL with Code::Blocks [duplicate] - c++

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).

Related

How to include and use any of the stuff in the subdirectories of `C:\msys64\mingw64\include`

In my VSCode environment with working msys2 / mingW64 /gcc compilers i'm now trying to use ncurses / curses.
msys is in the default installpath, so gcc / g++ /gdb are in
C:\msys64\mingw64\bin
As i understand it, the headerfiles being used by directives are the ones in
C:\msys64\mingw64\include.
So in general i think this is a question how to properly include and use any of the stuff in the subdirectories of C:\msys64\mingw64\include
For my case with ncurses, in include directory there are two subdirectories with names ncurses and ncursesw with identical content.
To begin, i try with a very simple `helloCurses.cpp file
#include <ncurses/curses.h>
using namespace std;
int main(int argc, char ** argv)
{
// init screen and sets up screen
initscr();
// print to screen
printw("Hello World");
// refreshes the screen
refresh();
// pause the screen output
getch();
// deallocates memory and ends ncurses
endwin();
return 0;
}
obviously the compiler can find the ncurses/curses.h file.
But it still doesn't compile.
I'm getting the error messages:
Executing task: C/C++: MingW g++.exe build active file
Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.cpp -o D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Mathias\AppData\Local\Temp\cc44BFtA.o: in function `main':
D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:7: undefined reference to `__imp_initscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:10: undefined reference to `__imp_printw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:13: undefined reference to `__imp_refresh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_stdscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_wgetch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:19: undefined reference to `__imp_endwin'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
As far as I understand the linker will not find the ncurses library. This should be libncurses.a or libncurses.dll.
These files are in the directory C:\msys64\mingw64\lib.
In the end i like to compile with VSCode tasks.json, but to begin with, the commandline
gcc -I C:\msys64\mingw64\include\ncurses -L C:\msys64\mingw64\lib -lncurses helloCurses.cpp -o helloCurses.exe
didn't work either and still gives me the same errormessage.
I have been pointed to
Why does the order in which libraries are linked sometimes cause errors in GCC?
I'm sure this will at some point become valuable information, but right now this isn't about the order of a multitude of probably codependent libraris. This is just about how to include a very first additional library not being basic enough to be includd by a simple
#include <stdio.h>, but still basic enough to be in the full toolchain being installed bypacman -S --needed base-devel mingw-w64-x86_64-toolchain

Visual Studio Code can't find reference to function [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 last month.
I'm trying to use Visual Studio for a specific project but I can't get files to link properly.
When including a header file with a defined function in another cpp file im getting an error undefined reference to testFunc() collect2.exe: error: ld returned 1 exit status
Thing is, this exact same code works perfectly in Eclipse. Can someone tell me what I'm doing wrong?
Test.cpp
#include "Other.h"
int main(){
testFunc();
return 0;
}
Other.h
#pragma once
void testFunc();
Other.cpp
#include "Other.h"
#include <iostream>
using namespace std;
void testFunc(){
cout << "HelloWorld";
}
When Buildung, this occours:
Starting build...
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
C:\Users\johan\AppData\Local\Temp\cck3aAZo.o: In function `main':
C:/Users/johan/cu-workspace/TEst/Test.cpp:5: undefined reference to `testFunc()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
If you build Other.h and Other.cpp as a project, then you need to configure the linker to add Other.lib into test project.
For a simple scenario, you can have all 3 files in one project and they should build just fine.
According to your build info:
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
You can see that Other.cpp is not in your project, so you might need to add it into your project.
Since you are using VS code, you can write a simple command in terminal to build your code:
C:\MinGW\bin\g++.exe -g C:\Users\johan\cu-workspace\TEst\Test.cpp C:\Users\johan\cu-workspace\TEst\Other.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe

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).

SDL2 error messages won't go away

Compile command/what's in the .bat:
g++ -o program.exe SME.cpp -w -Wl -subsystem,windows -lmingw32 -lSDL2main -lSDL2
pause >nul
SME.cpp:
#include "SDL2\SDL.h"
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
I have build.bat, SME.cpp and SDL2.dll in a folder
MinGW is in the default C:\MinGW directory
I installed MinGW using the installer
I added C:\MinGW\include
I put the SDL2 header files in C:\MinGW\include\SDL2\
I put the SDL2 library files (SDL2main.lib and SDL2.lib) in C:\MinGW\lib.
I added C:\MinGW\bin to the Path variable
I've solved all of the previous problems by looking on the internet for answers to them
The compiler keeps complaining that there is an undefined reference to SDL_Init and SDL_Quit and that ld.exe (I have no idea what that is) has returned exit status 1 due to an undefined reference to #WinMain16.
I've searched everywhere and there is a short supply of answers to this problem and all of the answers I've come across haven't worked. I've tried 3 different ways of doing this: with a .bat, with a Makefile and with Eclipse. It always comes down to these three errors. I have no idea what is going wrong, I checked and SDL_init.h and SDL_quit.h both exist. I don't know what #WinMain16 is either.

Compiling SDL2 with mingw using sublime text 2 errors

I am trying to compile SDL2 with mingw and I get this error:
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
I googled this error and it looks like I need to specify to link with the -mwindows tag but that doesn't work. Here is my batch file:
g++ -o Game.exe Main/Main.cpp -lmingw32 -LC:\MinGw\include\SDL2\lib\x86 -mwindows -lSDL2main -lSDL2
pause
start /d "C:\Users\Mathew Bergen\Documents\Programming\C++\LD Practice" Game.exe
How do I fix this error?
int main(int argc, char** agv){
//code here
}
For your main function, most of the time I've seen that link error this was the problem. I know I'm really late at posting an answer it's just for other people who have the same problem and stumble upon this page