0

I'm making an app with DragonFireSDK and I want to organize my multi thousand line app with .cpp and .h files

I get tons of errors when trying to do stuff though

So my app.cpp (main, required one) looks like this

Code:

#include "DragonFireSDK.h"

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
#include "SaveData.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Functions.cpp"
#include "AppMain.cpp"
#include "AppExit.cpp"
#include "OnTimer.cpp"

The #include "SaveData.h" through #include "Variables.h" all have something like Code:

#ifndef _HeaderName
#define _HeaderName
//STUFF HERE LIKE
#define player1 0
#define player2 1
//OR
typedef struct _number {
    int view;
    int number;
    bool able;
    int opacity;
};_number number[4];
//OR
int whoseturn;

int bet[5];
bool reachedmax[5];

int playerimg[5];
#endif

Now I may be doing something wrong already but here's some more... My AppMain.cpp, OnTimer.cpp etc look like this (AppMain(), etc are required functions too) Code:

#include "DragonFireSDK.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Definitions.h"
#include "Structures.h"
#include "Variables.h"
#include "SaveData.h"

#include "Functions.cpp"

void AppMain() {
//STUFF HERE
};

Now this is where I think the problem is... Functions.cpp Code:

#include "DragonFireSDK.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

void SavePlayerMoney() {
    //...
}
void   SetInfo(int idnum, bool actuallyset) {
      //...
}
void   SwitchButton(int idnum, bool makeactive) {
      //...
}
void   DisableButton(int idnum){
      //...
}

Now the errors I get after I thought if fixed all the stuff... Code:

1>AppMain.obj : error LNK2005: "void __cdecl SwitchButton(int,bool)" (?SwitchButton@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "double __cdecl round(double)" (?round@@YANN@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "void __cdecl SetInfo(int,bool)" (?SetInfo@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "int __cdecl Digits(int)" (?Digits@@YAHH@Z) already defined in App.obj

Any help is very greatly appreciated!

3
  • 1
    Avoid macros and preprocessor #define player1 0. Don't #include .cpp files. Avoid globals and prefer encapsulation with class objects. Actually just pick up a good C++ book when you get the chance. Commented Apr 6, 2011 at 23:05
  • 1
    Despite the c++ tag and *.cpp filenames, this appears (from what we can see) to be a pure C program. Commented Apr 6, 2011 at 23:09
  • Missing some basic concepts. Seriously consider getting an IDE like Visual Studio and a book like this: amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326973 Commented Apr 6, 2011 at 23:18

4 Answers 4

4

Don't #include the .cpp files.

The C compilation model is that each function is defined precisely once, i.e. in exactly one compilation unit (i.e. one object file). You compile each source file independently into a separate object file (#include-ing header files so that the compiler knows e.g. the prototype of functions to be used). You then link these separate object files together to form the final executable.

If you #include the .cpp files, you will end up with the same function being defined in multiple compilation units (remember that #include is basically equivalent to copy-pasting the contents into the file that's doing the including). So the linker will get confused, and give you the messages that you are seeing.

UPDATE

Oh, I see the problem is that you don't have a corresponding header file for Functions.cpp. The idea is that you also write a Functions.h, along the lines of:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void SavePlayerMoney();
void SetInfo(int idnum, bool actuallyset);
void SwitchButton(int idnum, bool makeactive);
void DisableButton(int idnum);

#endif

And then you #include this header file, rather than the .cpp file.

Sign up to request clarification or add additional context in comments.

10 Comments

Then what do I do with them, they have functions needed for the app and AppMain.cpp
Do I just change the extension or something?
@Mark: Only #include the header files (and indeed, only the ones that are relevant). As to how you link this all together, it depends how you're building your project. If you're using an IDE, it usually just handles this for you. If you're using e.g. GCC at the command line, you need to something like g++ file1.cpp file2.cpp -o my_app.
As I said below-where do I include Functions.cpp?
@Mark: You don't (well, you shouldn't) #include .cpp files. You compile each one separately, and then link them together.
|
2

The linker complains because functions are defined more than once. A function may only be defined in one translation unit (cpp file, after compilation it becomes an obj file) - except if it is declared inline.

You're including Functions.cpp in other units, so the function definitions from Function.cpp get duplicated into those, thus causing the linker trouble.

The solution would be to declare the functions inline - or, even better, declare them in a header (i.e. Functions.h) and define them in Functions.cpp. Any users of those functions may then #include Functions.h and have access to these functions even though they don't know their implementation.

To declare a function, do: int foo();, to actually define it, do int foo() { your code goes here}.

5 Comments

Ok sounds like this may be it but one question... They have default values...will it complain about redefinition of default values
Well...I'll find out in a second :)
Actually have one more question-where do I include the Functions.cpp where they are defined?
@Mark you out your default values in the header file with the declaration of the function prototype, then in the cpp side with your function implementation you define the parameters without the = value part.
You don't include the Functions.cpp. It gets compiled separately. The linker (the tool that gave you those nasty errors) ultimatively resolves references to functions defined in other translation units.
2

I think everyone answered this really well so I'm just going to give you my C++ philosophy on big projects because it seems like it is information that you may find useful.

ALWAYS separate function declarations and implementation. It will make your life considerably easier. Declare function prototypes in a .h file, then write the implementation in a .cpp file.

For example:

// mystuff.h
#ifndef MYSTUFF_H
#define MYSTUFF_H

int myFunction(int value, char letter);

#endif

And in my .cpp file:

// mystuff.cpp

#include "mystuff.h"

int myFunction(int value, char letter) {
    // insert implementation here
}

Why do this? Well one great reason is that when your code doesn't work (as it ostensibly will, an inescapable reality for any programmer), you can substitute out your .cpp file with alternate implementations without modifying the structure of your code. Not only that, there are various tricks you will discover that will rely on separating declarations and implementation that will ease your life considerably. Bottom line, do it.

Attempt encapsulation wherever possible. If you're doing a big project (and you will notice this is true for most big projects you encounter), encapsulating similar functions, variables, and the like will save you considerable time and energy. It seems like you're making a program to play a game- have you thought about encapsulating each player into a Player or Human class, with class-specific functions for each one? If you're a C++ or Java junkie like myself, you will find that an object-oriented approach is the most effective approach 99 times out of 100 (the 1% of situations is usually where you have helper functions that don't really fit in any of the objects you've defined).

Also, encapsulation enables you to take advantage of the two other fundamental principles of object-oriented design- polymorphism and inheritance. For example, you could define a Player class, then if your game involves a computer player and a human player, you could write a separate class for each of them that inherits the basic functionality of a Player but implements each function of a Player in a different way (i.e. if there is a makeMove function, you would have a different implementation for a human than a computer. Thus, inheritance greatly simplifies your job). There are obviously many qualities of OO design that are appealing, but for what I've gleaned from your code, I'd say you would benefit the most from these ones.

Obviously, this is my own philosophy and not one that I wish to forcefully impose on you. But hopefully you will take a few helpful tips out of my terse rambling to improve the way you write code and/or avoid long lists of errors. Best of luck!

2 Comments

While there's some good advice buried in here, this isn't really an answer to the question.
There is good advice and helps me understand more: +1 so it's not negative!
1

Move your function declarations to header files. For example, looks like Functions.h should contain:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

#endif

Then Functions.cpp can just include Functions.h instead of those declarations. Some header files may need to include other header files to get the appropriate types.

Finally, never #include a *.cpp file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.