Sign in to follow this  

Can't split code across files

Recommended Posts

GuestUnit    122
EDIT: Now I just can't tell both source files that HWND hWnd exists without getting a multiple definition or undeclared identifier error. I'm trying to split my code up into multiple files. I managed to put the function declarations/prototypes into a header. When I put the function definitions into a .cpp and edit the other files so that they include the function definitions .cpp, I get 173 errors of undeclared identifiers. What I think I'm supposed to be doing is create a .cpp containing the definitions, a .h that simply says #include "(function definitions).cpp", and have the other files #include the (function definitions).h. It also includes the function prototype.h. Thanks if you can help me successfully put the function definitions into a .cpp and compile. [Edited by - GuestUnit on August 18, 2008 9:24:00 PM]

Share this post

Link to post
Share on other sites
GuestUnit    122
00func def.h

#include "00func def.cpp"

0func proto.h

// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void initDInput(HINSTANCE hInstance, HWND hWnd); // sets up and initializes DirectInput
void detect_keys(void); // gets the current keys being pressed
void cleanDInput(void); // closes DirectInput and releases memory

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

00func def.cpp

#pragma once
#include "stdafx.h"
#include "0func proto.h"
#include "00func def.h"

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface

D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = FALSE; // program fullscreen, not windowed
//Make TRUE for windowed, obviously
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer

// create a device class using this information and the info from the d3dpp stuct


// this is the function used to render a single frame
void render_frame(void)
// clear the window to a deep blue
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

d3ddev->BeginScene(); // begins the 3D scene

// do 3D rendering on the back buffer here

d3ddev->EndScene(); // ends the 3D scene

d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen


// this is the function that initializes DirectInput
void initDInput(HINSTANCE hInstance, HWND hWnd)
// create the DirectInput interface
DirectInput8Create(hInstance, // the handle to the application
DIRECTINPUT_VERSION, // the compatible version
IID_IDirectInput8, // the DirectInput interface version
(void**)&din, // the pointer to the interface
NULL); // COM stuff, so we'll set it to NULL

// create the keyboard device
din->CreateDevice(GUID_SysKeyboard, // the default keyboard ID being used
&dinkeyboard, // the pointer to the device interface
NULL); // COM stuff, so we'll set it to NULL

dinkeyboard->SetDataFormat(&c_dfDIKeyboard); // set the data format to keyboard format

// set the control you will have over the keyboard

return; // return to WinMain()

// this is the function that detects keystrokes and displays them in a message box
void detect_keys(void)
static BYTE keystate[256]; // create a static storage for the key-states

dinkeyboard->Acquire(); // get access if we don't have it already

dinkeyboard->GetDeviceState(256, (LPVOID)keystate); // fill keystate with values

/*This was part of the example. It seems this is where key functions go.
if(keystate[DIK_A] & 0x80) // if the 'A' key was pressed...

// then inform the user of this very important message:
MessageBox(NULL, L"You pressed the 'A' key!", L"IMPORTANT MESSAGE!", MB_OK);*/

if(keystate[DIK_ESCAPE] & 0x80)//The 0x80 is a bool to check if it's pressed or not
//Don't worry about it
PostMessage(hWnd, WM_DESTROY, 0, 0);

// this is the function that closes DirectInput
void cleanDInput(void)
dinkeyboard->Unacquire(); // make sure the keyboard is unacquired
din->Release(); // close DirectInput before exiting


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D


Multi cpp test

// Multi cpp test.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include "Multi cpp test.h"
#include "0func proto.h"
#include "00func def.h"
#define MAX_LOADSTRING 100

//Include windows, DX9, Dinput, C++ lib, and file streaming
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <cstdlib>
#include <fstream>

// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
//include DirectX input files
//#pragma comment (lib, "dinput.lib") This causes an error for some reason
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECTINPUT8 din; // the pointer to our DirectInput interface
LPDIRECTINPUTDEVICE8 dinkeyboard; // the pointer to the keyboard device

HWND hWnd;
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)


ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX); = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW; // not needed any more
wc.lpszClassName = L"WindowClass";


hWnd = CreateWindowEx(NULL,
L"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
//Replace with WS_OVERLAPPEDWINDOW for windowed
0, 0, // the starting x and y positions should be 0
//Those are the starting x and y positions
SCREEN_WIDTH, SCREEN_HEIGHT, // set the window to 640 x 480
//Simply change by changing values
//Or even replacing the names with numbers

ShowWindow(hWnd, nCmdShow);

// set up and initialize Direct3D

// set up and initialize DirectInput
initDInput(hInstance, hWnd);

// enter the main loop:

MSG msg;

DWORD starting_point = GetTickCount();

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
if (msg.message == WM_QUIT)



while ((GetTickCount() - starting_point) < 1.6);

// clean up DirectX and COM
// Clean up DirectInput
return msg.wParam;

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return 0;
} break;

return DefWindowProc (hWnd, message, wParam, lParam);

Turns out I forgot to put the WindowProc() into the func def.cpp.

Share this post

Link to post
Share on other sites
pi_equals_3    517
You're still doing this incorrectly. Including a .cpp file is a giant red flag that you're organizing files the wrong way. Go read the article rip-off linked.

Share this post

Link to post
Share on other sites
GuestUnit    122
The only error that's preventing me from compiling (now) is that another .cpp is saying that hWnd is an undeclared identifier.

Thing is though, the format hWnd is in is HWND hWnd (typedef HWND__ *hWnd).

Obviously, when I put HWND hWnd into the complaining .cpp I get a multiple definition error.
When I make HWND hWnd into its own header, I get a multiple definition error again.

I try to make it an extern as according to the tutorial, but I seem to be doing something wrong without any idea to make it correct.

Share this post

Link to post
Share on other sites
oler1s    585
The problem previously was that you completely mucked up source organization. You can get all sorts of errors undeclared or multiple declaration errors if you don't fix that part first.

For example, are you still trying to include a cpp file anywhere? Do you understand why cpp files are not included?

Share this post

Link to post
Share on other sites
GuestUnit    122
Yeah, I understand why .cpp files aren't included.

I interpreted the information wrong, that's all. I thought that a .h should include a .cpp since each .cpp should have a .h instead of that .h just basically tell the .cpp files that the object exists.

The simple problem here now is that I can't figure out how to get both .cpp files to recognize HWND__*hWnd without defining or declaring it multiple times.

I was thinking about making a new external variable (probably a pointer), assigning it hWnd's value, then assigning it to the complaining function.
But something feels wrong with this...

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this