Skip to main content
gamedev.net
🔒 Locked

Problem linking .cpp files in a project

Started by ErayMan Mar 20, 2003 at 8:00 PM 3 replies 750+ views
Original Post
ErayMan
ErayMan
Hi there I want to split my main.cpp (using VC++ 6.0) file into smaller ones, because it is getting pretty big and it is very confusing. So i''d make something like init.cpp, where it creates my window, an draw.cpp, where i''d do all my drawing, etc the problem is that i can''t make them communicate between them. In main.cpp, i put: DrawTile(); which is defined in draw.cpp, but on compil, it says "undeclared identifier" All the .cpp files are included in my project, and they are all compiled, so how can i make each of them communicate? Also, can i create a global variable in one, and access it in the other? thanks -ErayMan
Qa303asmGuru
Qa303asmGuru
1.) Create a file called "draw.h" and put the (type) DrawTile() prototype in this header file. Be sure to check for multiple inclusions i.e.
#ifndef DRAW_H
#define DRAW_H
.
.
.
#endif

2.) Yes. Say you want to use a global var someVar in main.cpp and in draw.cpp. Declare someVar in main:

(type) someVar;

and in draw.cpp or draw.h:

extern (type) someVar;


Hope that helps ya out.

-Q

[edited by - Qa303asmGuru on March 20, 2003 9:31:26 PM]

[edited by - qa303asmguru on March 20, 2003 9:32:24 PM]
ErayMan
ErayMan
Thanks a lot, it worked like a charm!

I started to split my functions into different files and all
I encountered another problem:
From main.cpp, i can call drawtile() from draw.cpp
but from draw.cpp, i can''t call buildtexture() from main.cpp
it says:
Draw.obj : error LNK2001: unresolved external symbol "int __cdecl BuildTexture(char *,unsigned int)" (?BuildTexture@@YAHPADI@Z)
Debug/DX.exe : fatal error LNK1120: 1 unresolved externals

in Main.cpp :
#include "Draw.h"
#include "Main.h"
int BuildTexture(char *, GLuint);

in Draw.cpp:
#include "Main.h"
#include "Draw.h"

Once again, i need your help!
thanks again
-ErayMan
Qa303asmGuru
Qa303asmGuru
Ah yes the wonderful ''unresolved external symbol''

It looks like you put the prototype:

int BuildTexture(char *, GLuint);

in main.cpp. This needs to be in main.h. Also make sure that the return types and parameters match up for BuildTexture.

Hope that helps...I''m actually wondering what kind of app (game?) you''re working on now...

Hope that takes care of that issue.

-Q
ErayMan
ErayMan
Well, thanks a lot
i made a mistake in my earlier post, i had put the function
int BuildTexture(char *, GLuint);
in Main.h already, but still it wasn''t working

But when you said :
"Also make sure that the return types and parameters match up for BuildTexture"

it gave me an idea
I replaced
int BuildTexture(char *, GLuint);
with
int BuildTexture(char *szPathName, GLuint &texid);

eh, the full thing, and now it works, but i''m not sure why hehe

well, thanks a lot

btw, i''m working on a 2d rpg engine
but i''m beginning OpenGL, so i''m going one step at a time, but I know a fair amount of C++, it''s just been a long time since I haven''t done any...
well i''m working on a layer for maps system and an entity class which will be able to do, well, anything you''d like to do with entities hehe
i''m just beginning, though, so it''s more to get some practice using classes and all, not to make a full engine, although you never know where it''ll lead hehe
well, in any case, thanks for your help and interest, see ya!
-ErayMan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.