Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Projects with several .cpp files

Author
Message
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 30th Sep 2005 20:41
Hello,

I'm trying to use several .cpp files in vs2003.NET, I have some errors in the added .cpp, as if it wouldn't recognize previously declared things.

Main file:


Added .cpp file:



Errors building:



So it does not find the var "a", defined at the main file, neither the command dbSync().

I can fix the var problem adding "extern":



but I'm stucked with getting the command to work

Please a little help for a newbie (someday we all were).

Thanks in advance.
MiR
21
Years of Service
User Offline
Joined: 13th Jul 2003
Location: Spain
Posted: 30th Sep 2005 20:45
As I understad it each cpp file is seperate to another. If you want to use DGDK commands in both files you will need to include it for both files. The same with the variable a. What you did when you declared it as extern was say that it´s defined in another file.

My signature has been erased by me because it's LARGE.
Thank you for the votez!1!
Sephnroth
22
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 30th Sep 2005 21:08
Indeed, a very rough analogy would be to consider each .cpp file as a different house and each header (.h) as a phone, or maybe phone number. For one to talk to the other it needs the right header.

you will need to #include "DarkSDK.h" in any cpp that uses sdk commands.

To use functions from one cpp file in another you need to prototype the function in a header and #include the header in the cpp you wish to use the function in, example:

moo.cpp:


moo.h:


main.cpp:


that would call the moo() function from moo.cpp and use it to print the result 4. if main.cpp DIDNT include moo.h then it would complain that moo() was undeclared.

variables can be sort of prototyped in a simular way using the keyword "extern" which stands for external. its like an uber global.

declare your variable like normal in moo.cpp, say
int a = 5;
then in moo.h you add:
extern int a;

then any cpp file which you want to have access to a you would include moo.h so it can read the extern declaration.

remember, the proper declaration in the cpp (not the extern in the header) should be declared in global scope, not inside a function. Personally i think you should minimise externed vars because it can quickly get messy. It might be worth externing all global vars from all cpp files in a Globals.h as thats one way to make it more tidy.

Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 1st Oct 2005 15:17
Quote: "
you will need to #include "DarkSDK.h" in any cpp that uses sdk commands
"


Now it works fine, but I do not why the first time I tried it raised some double declaration errors...

Well, thanks! I'm gonna keep on coding
Troll Fiddler
19
Years of Service
User Offline
Joined: 19th Jan 2005
Location: Mayo, Ireland
Posted: 3rd Oct 2005 12:01
If you import the same header into a couple of different files you will get a double definition error. The standard way around it is to use some variable and only include the rest of the header file if the variable isn't defined. You can do something like this

#ifndef SPELL_H
#define SPELL_H

<all your header stuff here>

#endif

That way the first time it's found it will be included, but not again by other files in the same compilation. No more double defs.

T.
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 3rd Oct 2005 12:09
You can also use the #pragma once pre-processor command

Come to the third DarkBasic Pro Sci Fi Con - Be there and be square
Blog:http://spaces.msn.com/members/BouncyBrick/
Web Site:http://www.nicholaskingsley.co.uk
Gen
20
Years of Service
User Offline
Joined: 6th Jul 2004
Location: Oklahoma, USA
Posted: 10th Oct 2005 08:34
Quote: "You can also use the #pragma once pre-processor command"


That is what i use most often...

DARK IDE RETURNS - Yes Dark IDE is still a go!!

Login to post a reply

Server time is: 2024-11-19 02:31:57
Your offset time is: 2024-11-19 02:31:57