1
votes

So I've been working on this for the better part of two hours and although I appear to be following the exact instructions of every forum/guide on the internet, I'm still getting linker errors trying to use directX with Visual Studio 2010.

Here is the code I'm starting with:

#include <D3DX10.h>  
#include <iostream>   
using namespace std;

ostream& operator<<(ostream& os, D3DXVECTOR3& v){  
      os << "(" << v.x << ", " << v.y << ", " << v.z << ")\n";     
      return os; 
}

int main (){  
    return 0;  
}

I have the SDK downloaded and installed and I have the manually set up the appropriate include and library directories in the project configuration properties. I have also set up additional linker input dependencies:

d3dx10.lib
d3dx10d.lib

However, I am still getting the following errors upon compiling:

1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain@16 referenced in function __tmainCRTStartup

1>C:\Users\Ben\Documents\Visual Studio 2010\Projects\DX Practice\Debug\DX Practice.exe : fatal error LNK1120: 1 unresolved externals

Any and all help is appreciated.

EDIT: Changed int main() to int WinMain(). New errors:

1>c:\users\ben\documents\visual studio 2010\projects\dx practice\dx practice\main.cpp(10): warning C4007: 'WinMain' : must be '__stdcall'

1>c:\users\ben\documents\visual studio 2010\projects\dx practice\dx practice\main.cpp(10): error C2731: 'WinMain' : function cannot be overloaded

1> c:\users\ben\documents\visual studio 2010\projects\dx practice\dx practice\main.cpp(10) : see declaration of 'WinMain'

EDIT2: Figured it out -

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

Thanks all for the help :)

2
I never said that WinMain was defined the way main is.Nicol Bolas
Why do you need to use WinMain in first place? Just keep it main. Change the linker setting: Linker->System->SubSystemAjay

2 Answers

4
votes

Unless you use a special setting in Visual Studio, windowed executables start with the WinMain function, not the regular main function. So you should either be creating a console app, be using WinMain, or use the setting to use the regular main function.

Note that starting with WinMain is not required for actually creating windows. It's just a Visual Studio convention.

The option to use regular main is under "Linker->Advanced" in the Project Settings dialog. It is called, "Entry Point", and to use the regular main, you use "mainCRTStartup" as the value.


If you insist on using WinMain, then you need to define it correctly:

int WINAPI WinMain( HINSTANCE   hInstance,          // Instance
                   HINSTANCE    hPrevInstance,      // Previous Instance
                   LPSTR        lpCmdLine,          // Command Line Parameters
                   int          nCmdShow)           // Window Show State
1
votes

If you include the Windows headers, then the linker expects you to provide a WinMain function, not the regular main. The WinMain entry point provides Windows-specific data like HINSTANCEs.