Unresolved External _main in a console application - c++

I have the following main function:
int main(int argv, char** argc) {
MainGame mainGame;
mainGame.run();
system("pause");
return 0;
}
it throw a LNK2019 unresolved _main at me.
now i did a bit of google-ing and found countless examples of people having accidentlyt setup win32 applications instead of the intended console app, so I checked mine in linker->system->subsystem and it read console.

Had a similar problem when using SDL. I've added #undef main after #include "SDL.h" since main was defined inside SDL for some other purpose (as DimChtz pointed out in the comments). It solved the problem.
By the way, this does not have to be SDL-specific. Some other included header or source file in your project could also be #define-ing "main", which would trigger this behavior.

Related

C++ Compiling windows service with visual studio

hope you could help me. I got a C++ source for my service based on Microsoft example, still i get linker error compiling it:
error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
as the entry point for windows services is int _tmain(int argc, TCHAR* argv[])
In my case it's void __cdecl _tmain(int argc, TCHAR* argv[]) { ... }
There are 1 header and 1 cpp files with a class used by the service and main.cpp contaning entry point and c style service related code. Subsystem is console without any custom entry point set. Still if i add classic int main(...) to the code project compiles yet the service does not start from windows service manager returning error.
Please advise how to compile this using _tmain.
Ok, still no luck with _tmain. Unfortunately i haven't tried to define WINAPI WinMain entry point.
Still if you proceed with wmain program entry point and add code mentioned by Richard Critten for a service startup SCManager performs just fine. For a complete service sample refer to MS doc.wmain will force you to use Unicode yet it shouldn't be such a problem in 2020.
Subsystem should be set as supposed to your needs and no need to set custom entry point.
Thanks to everyone commented.
UPD You have to include tchar.h in order to use _tmain, so all types of entry points will work fine.

Linker error _SDL_main unresolved in _main_getcmdline [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 4 years ago.
I'm following LazyFoo's tutorial for SDL2. I went through the first ten lessons, and now i'm getting this error (until i compile i don't get any errors from VS).
1>------ Inizio compilazione: Progetto: yetANewSDLProj, Configurazione: Debug Win32 ------
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: riferimento al simbolo esterno _SDL_main non risolto nella funzione _main_getcmdline
1>Compilazione progetto "yetANewSDLProj.vcxproj" NON COMPLETATA.
========== Compilazione: 0 completate, 1 non riuscite, 0 aggiornate, 0 ignorate ==========
"Reference to external symbol "_SDL_main" unresolved in the function "_SDL_getcmdline"
It seems to me that the error is not in my code but rather between SDL's files but i can't figure out which file supposedly Visual Studio doesn't find or why.
I created a new empty project, the only code i have (one file, main.cpp) is:
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
while(1){}
SDL_Quit();
return 0;
}
The include files are in the path: "C:\vs_dev_lib\include"
The DLL/lib files: "C:\vs_dev_lib\lib\x86"
Both paths are inserted in "VC++ Directories" (Include Directories / Library Directories).
I also copied all DLLs in both System32 and SysWOW64.
I also tried copying DLLs and .lib file in my project directory.
The headers, dll and lib files are from SDL2, SDL2 image, Mixer and TTF (in all cases development libraries for windows (Visual C++ 32/64-bit).
Linker/Input/Additional Denpendencies look like this:
SDL2.lib
SDL2main.lib
SDL2_image.lib
SDL2_ttf.lib
SDL2_mixer.lib
Linker/System/Subsystem is set to "Console(/SUBSYSTEM:CONSOLE)"
Character set is "Multibyte character set" (translated, not sure how exactly it's spelled in english).
Thanks in advance, if you need any other info let me know.
Googling didn't bring me any luck, i've been stuck with this for the past two days :/
Try defining #define SDL_MAIN_HANDLED prior to including sdl headers if you are going to manually provide an entry point.
#define SDL_MAIN_HANDLED
#include "SDL.h"
int main(int argc, char *argv[])
{
SDL_SetMainReady();
SDL_Init(SDL_INIT_VIDEO);
...

Allegro 5 Visual studio 2017 linker errors occuring

I've got some problems getting up Allegro 5 library in Visual Studio 2017. In most cases i'm getting linker errors (unknown/unrecognized external types).
I said in most cases, because this code:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv) {
ALLEGRO_DISPLAY *display = NULL;
if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if (!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
source:https://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Displays
compiles as usual without errors and runs as expected.
But this code:
#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_primitives.h"
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_DISPLAY_MODE disp_data;
al_init(); // I'm not checking the return value for simplicity.
al_init_image_addon();
al_init_primitives_addon();
al_get_display_mode(al_get_num_display_modes() - 1, &disp_data);
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(disp_data.width, disp_data.height);
al_rest(3);
al_destroy_display(display);
return 0;
}
source: the same, next tutorial.
gets me 3 errors in linker. Many other codes do as well with various numbers of errors in linker.
In this prticular case are the errors as folows:
Error code Description
LNK2019 Unrecognized external symbol __imp__al_init_image_addon reffered in function _main
LNK2019 Unrecognized external symbol __imp__al_init_primitives_addon reffered in function _main
LNK1120 Number of unrecognized external types:2
I've read that Allegro has some dependencies in freetype, so i installed it using NuGet(project pakage manager), the same way as Allegro.
I'm new to Visual Studio, so if you have a possible solution, please explain step by step (and, if you are capable of it, please explain, so I can learn something new)
If you need something to find the solution, just ask and explain how can i get it.
Thanks a lot in advance.
Yep, I'm just stupid.
I'm sorry for all the trouble I've caused.
It was because I didn't know that I have to enable the modules in project properties under Allegro 5 tab. Now I've got it up and running.
I'm very sorry for bothering you.
Glad you solved it. For the record, the addon libraries are not linked by default in the Nuget Package for VS. After installing the Allegro 5 Nuget package for your solution, right click on the project properties and select Allegro5 from the menu on the left. Go to addon libraries, and enable the addons you're using. Easy peasy.

linking vs 2012 c++ GDAL

I created a standard windows application with VS 2012 Pro. Just a main.cpp that looks like this:
#include "gdal_priv.h"
#include "cpl_conv.h"
int main(int argc, char* argv[])
{
GDALAllRegister();
return 0;
}
I have set my include path properly. I have gdal_i.lib in my Linker -> Input -> Additional Dependencies.
Link fails with the following message:
1>main.obj : error LNK2019: unresolved external symbol
_GDALAllRegister#0 referenced in function _main
(A scad of other symbols are missing as well, but this one should be easy.)
I used dumpbin and GDALAllRegister appears in the exported symbols. It does appear as "GDALAllRegister", not as "_GDALAllRegister#0".
I tried downloading and using the dev build, and also built myself. Same results.
I just know this is something simple, but I'm totally brain-cramping here. What have I done wrong?
Thanks.
-reilly.
I said it was simple and stupid, and it was.
I had the build manager set to build 32 bit.
Another 4 hours of my life I'll never get back.
Thanks to Manuell for his suggestion. It got me looking in a different direction and I found it.
Sorry, community. This is what comes from programming tired.
-reilly.
This kind of error is typical of a "Calling Convention" mismatch between EXE and LIB.
Check you have __cdecl (/Gd) in "Configuration Properties" -> C++ -> Advanced -> "Calling Convention"

why I can't use normal C++ classes with Qt

Can anyone pls tell me that, why I can't use normal C++ classes within a Qt programme. If there is any class which aren't inherited from QObject the compiler give me a linking error called,
error LNK2019: unresolved external symbol _main referenced in function _WinMain#16
I'm using Qt 4.5.2 (compiled by myself) with vs2005. Pls help me to solve this !
Edit:
Example...
//UnitManager.h
class UnitManager
{
public:
//-Some code
};
//CivilizationViewer.h
class CivilizationViewer : public QMainWindow
{
Q_OBJECT
//-some code
};
//main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CivilizationViewer w;
w.show();
return a.exec();
}
If I include UnitManager.h in CivilizationViewer.h compiler will give me that error. (eventhough I include UnitManager.h in main.cpp compiler will give me the error)
The error you gave doesn't have anything to do with what classes you're using. It looks like it's related to the entry point you have set for your application. Usually you want to use main() instead of WinMain() in Qt programs. Make sure your configuration is set up right.
You included a little bit of code in your question. Is that everything? If so, you're missing a main function.
Thanks for everyone. I found the error.
There is SDL.h in the UnitManager.h, so I have to add SDL.lib and SDLmain.lib (it is correct, right ?) then there is another definition for main in SDLmain.lib. So, there was a coflict between definitions of main. Therefore I added SDLmain.lib before adding qtmaind.lib. Then the problem solved by only giving a warning called,
warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
What is that warning ? I can just ignore the warning, but I like to get to know it ! Thanks
It seems like you need to link with qtmain.lib (qtmaind.lib for debug builds).
That library provides a WinMain function which is needed if you declared /subsystem:windows.
Source: http://lists.trolltech.com/qt-interest/2005-12/thread00170-0.html
It looks like you're not including the QT libraries properly... (the actual .lib file)
I think you actually made a win32 app.
try replacing your main for:
int _tmain(int argc, _TCHAR* argv[]){
your code
}
Seeing your error msg, i wouldn't guess Qt was your problem.
Did you install the Visual Studio Qt integration? Try it and make a new Qt project.