Handmade Hero»Forums»Code
107 posts
including only .h files
Edited by C_Worm on
Hey!

lets say i have some files:
win32main.cpp
game.h
game.cpp

how do i include ONLY the game.h in win32main.cpp like you do in Visual studio where you only include game.h in
game.cpp and win32main.cpp.

what casey does in "E21: loading code dynamically" is that he compiles a .dll and loads the code.

BUT is that what visual studio does under the hood in the IDE which makes it possible to just include the .h files where you want them?
OR am i missing something super trivial here?

i.e i want the declarations in game.h file, definitions in game.cpp file and ONLY include the .h file in win32main.cpp.

all the best :)
Mārtiņš Možeiko
2559 posts / 2 projects
including only .h files
If you want to include game.h into win32main.cpp then you write #include "game.h" somewhere inside win32main.cpp. That's it. Including files has nothing to do with dll files. It is about where you put #include directive, and you need to put it where you want it to be. IDE has nothing to do with this.
107 posts
including only .h files
No because then i get unresolved external linker errors.

However, it works in the IDE.

including the DX.cpp in main.cpp would work but i only want to include the DX.h.

im just putting in some arbitrary code here.




DX.h:
1
VertexBufferInfo CreateVertexBuffer(ID3D11Device *device, Vertex *vertices, UINT sizeVertice);


DX.cpp:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "DX.h"

VertexBufferInfo CreateVertexBuffer(ID3D11Device *device, Vertex *vertices, UINT sizeVertices)
{
	VertexBufferInfo vbInfo = {};	

	vbInfo.VBdesc.Usage = D3D11_USAGE_DYNAMIC;
	vbInfo.VBdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	vbInfo.VBdesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	vbInfo.VBdesc.MiscFlags = 0;
	vbInfo.VBdesc.ByteWidth = sizeVertices;
	vbInfo.VBdesc.StructureByteStride = sizeof(Vertex);

	vbInfo.subResData.pSysMem = vertices;

	HRESULT hra = device->CreateBuffer(&vbInfo.VBdesc, &vbInfo.subResData, &vbInfo.VBuffer);

	return vbInfo;
}


main.cpp:
1
2
3
4
5
6
7
8
#include "DX.h"
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE phInst, LPSTR cmdLine, int cmdShow)
{

VertexBufferInfo VBinfo = CreateVertexBuffer(device, vertices, izeVertice);

return 0;
}

Mārtiņš Možeiko
2559 posts / 2 projects
including only .h files
If you have function defined elsewhere (in DX.cpp file) then you need to link them together. Including file only provides declaration to compiler, so it can compile fine. But during linking the linker needs to resolve all function/variable names. So it needs to know where is function.

You need to either "include" all necessary definitions in your main .cpp file. Or you need to pass DX.cpp (or its .obj file) also to linker when compiling executable. What MSVC IDE is doing by default is compiling each .c/.cpp file independently and then passes all .obj files to linker to produce one .exe file.

This has almost nothing to do with #include. It is about how you call compiler and linker. Make sure you pass all necessary files to linker so it can find where are each function.

There is a great episode on how compiler & linkers work from Casey. You can see it here: https://hero.handmade.network/episode/chat/chat013/
107 posts
including only .h files
Allright, thanks!! :)