C++ won't compile multiple source files

Apr 4, 2009 at 11:48pm
Okay, I'm not really new to C++, but I can't figure this out. So far I've managed to make all my projects with only a single .cpp file, using as the main.cpp, and including header files.

My problem is that I can't get my compiler to link together two .cpp files, even the simpliest ones. For example I make a simple main.cpp, and than add another cpp to the project, make a double variable there, and trying to access it in the main.cpp's main () function.

Am I missing some really important thing, like some .h to include?

// main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main ()
{   
    asd = 7;
    system("PAUSE");
    return EXIT_SUCCESS;
}


// sub.cpp
 
double asd;


I'm using DEV C++ on windows XP, in project options compilation and linking is checked, I'm just freaking about not knowing what to do.
Apr 5, 2009 at 12:25am
Variables do not work that way. You will have to declare double asd; as an extern.

I think what you are trying to get at is prototyping functions:

http://www.cplusplus.com/doc/tutorial/functions2.html

You can put the prototypes in a seperate .h file, the definitions in a seperate .cpp, then use them by including the .h in your main.cpp file.
Apr 5, 2009 at 8:08am
This works for me. Hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//main.cpp
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

#include "sub.cpp" // include the sub.cpp file

int main ()
{   
    asd = 7;
    system("PAUSE");
    return EXIT_SUCCESS;
}

1
2
//sub.cpp
double asd;


Should work for you, assuming sub.cpp is in the same directory as main.cpp.

Your code almost worked, the only thing you were missing was including the file with the definition of that variable.

Good job, though!

Have a good one :-)
Last edited on Apr 5, 2009 at 8:12am
Apr 5, 2009 at 8:43am
Usually including cpp files is not a good thing, you should include a common header with declaration and Link cpp files together (As firedraco said).
Apr 5, 2009 at 9:51am
Bazzy, I agree.
But some might have an opinion that code duplication and OOP don't fit together.
Apr 5, 2009 at 9:56am
Thanks for the responses, one thing though.

I'm trying to do what firedraco says (put the prototypes in a seperate .h file, the definitions in a seperate .cpp, then use them by including the .h in your main.cpp file.) I always thought in a project .cpp files linked together in such a way they were like only one big file, accessing every other .cpp files's variables, and functions.

I guess extern would work, but than if I declare an object of a class in a .cpp, for every objects and variables I have to use extern keyword if I want to access them in another cpp?


EDIT:
Okay, I tried another thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// MATRIX.H

#include <GL/gl.h>
#include "unit.h"


#ifndef MATRIX_H
#define MATRIX_H

class MATRIX {
     // prototypes of functions, variables
             
} viewMatrix;

#endif 


1
2
3
4
5
// MATRIX.CPP

#include "matrix.h"

// decleration of MATRIX class functions (construktor, destructor, etc.) 


1
2
3
4
5
6
// MOUSE.CPP

#include "mouse.h"
// #include "matrix.h"

// decleration of MOUSE class functions, using and modifing viewMatrix object 


Okay, so my problem is here: If I don't include the matrix.h in mouse.cpp, I get an error msg so that viewMatrix hasn't been decleared. If I include the matrix.h, I get an error msg so that viewMatrix has been decleared multiple times. So wtf?
Last edited on Apr 5, 2009 at 10:42am
Apr 5, 2009 at 10:17am
Terrform said:
I always thought in a project .cpp files linked together in such a way they were like only one big file, accessing every other .cpp files's variables, and functions.

No!. Each cpp file is compiled strictly on it's own merit (unless you do the - not recommened thing - of #including cpp files in other cpp files).

Terraform also said:
I guess extern would work, but than if I declare an object of a class in a .cpp, for every objects and variables I have to use extern keyword if I want to access them in another cpp?

You need to read up on Global Variables and Scopes.
Apr 5, 2009 at 10:19am
Ookay, thanks tho I will do some research than if I still have the problem I'll check back.
Topic archived. No new replies allowed.