multiple cpp files in VC++

Started by
6 comments, last by CrimsonSandman 22 years, 4 months ago
Hi all, I usually program in linux so i''m sure this is a rudimentary question but in VC++ how do you get it to let you seperate your functions into seperate cpp files? For instance, let''s say i want to put DrawGLScene in a file by itself. In linux i would just have my main headers, a custom header for my global variables, a custom function header for function prototypes, and #include "draw.cpp", let''s say, that holds the DrawGLScene function. My problem is when i try to do the same in VC, i.e: #include #include the rest of the headers that would go here, including custom headers #include "draw.cpp" int WINAPI WinMain...rest of program, it comes back with the errors: c:\windows\desktop\winprogs\tetrivix\draw.cpp(5) : error C2065: ''GLvoid'' : undeclared identifier c:\windows\desktop\winprogs\tetrivix\draw.cpp(6) : error C2448: '''' : function-style initializer appears to be a function definition Error executing cl.exe. It seems like it would look at the GL headers that had already been defined. Anyway, any help is greatly appreciated. Thanks, Crimson
Advertisement
I might misunderstand you.
Do you have a "draw.h" and a "draw.cpp"?
If so make sure you are creating a project. and have the .cpp file include the header, dont have the header file include the .cpp file.
I guess you can do it either way, but i never have.
If you include the header that holds the prototype for DrawGLScene and put the function definition in a separate cpp file the file will automatically be included.

So you can have:

//header.h
int Function(int whatever); //Function prototype

//function.cpp
int Function(int whatever)
{
...
}

//main.cpp
headers...
#include "header.h"

int main()
{
int a = Function(3);
...
return 0;
}
just include the GL headers in your draw.cpp file
Isn''t that what projects are for?
quote:Original post by CrimsonSandman
Hi all,
I usually program in linux so i''m sure this is a rudimentary question but in VC++ how do you get it to let you seperate your functions into seperate cpp files?
For instance, let''s say i want to put DrawGLScene in a file by itself. In linux i would just have my main headers, a custom header for my global variables, a custom function header for function prototypes, and #include "draw.cpp", let''s say, that holds the DrawGLScene function. My problem is when i try to do the same in VC, i.e:
#include
#include the rest of the headers that would go here, including custom headers
#include "draw.cpp"

int WINAPI WinMain...rest of program,

it comes back with the errors:
c:\windows\desktop\winprogs\tetrivix\draw.cpp(5) : error C2065: ''GLvoid'' : undeclared identifier
c:\windows\desktop\winprogs\tetrivix\draw.cpp(6) : error C2448: '''' : function-style initializer appears to be a function definition
Error executing cl.exe.

It seems like it would look at the GL headers that had already been defined. Anyway, any help is greatly appreciated.

Thanks,
Crimson


How odd, you say you usually program for linux... Why not use the exact same way you use when creating a project with multiple files on Linux?

ie: your ".h" are included in one ".cpp" and when you compile, you compile with all the ".cpp". At least, that how I''ve always done it and how it''s usually done when I download tarballs... Oh well...





"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
[Cyberdrek | ]
Still no luck. Here''s a bit of my code. Maybe that will show what i''m doing wrong a little better

in main.cpp:
#include
#include
#include
#include
#include "globals.h"
#include "prototypes.h" // with the exception of DrawGLScene. Initially I had it in here and no draw.h but it didn''t work. Any functions in the local file will work with the functions defined by this header

#include "draw.h" // i''ve also tried draw.cpp in place of draw.h and just having a draw.cpp and no draw.h at all

// Entry Point for the program
int WINAPI WinMain...

in draw.h:
#ifndef _DRAW_H_
#define _DRAW_H_

int DrawGLScene(GLvoid); // GLvoid comes back as undefined; same errors as listed in my first post basically

#endif

in draw.cpp:
#ifndef _DRAW_CPP_
#define _DRAW_CPP_

#include "draw.h"
// Here''s where the drawing takes place
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

return TRUE;
}

#endif

I started off by making a project. As you can see in the above code, i''m just trying to get lesson 1 to work with seperate files right now. Also, if I include the gl headers in draw.cpp, the compiler complains of errors in gl.h(because it''s being called twice. If I only call it in draw.cpp, then the functions in the main.cpp file have a lot of undefines(glvoid, etc.)

Thanks,
Crimson
It works exactly the same way as it works in linux actualy.

Let me give an example ?? I am sure you know this.

// header file draw.h

#ifndef DRAW_H
#define DRAW_H

// include gl.h
#include

GLvoid Draw(...);

#endif

//end of draw.h header file

------------------------------
// draw.cpp
#include "draw.h"
//include other header files required here
GLvoid Draw(...)
{
// your code goes here
}

// end of draw.cpp
---------------------

// the main.cpp file
// include windows specific stuff & other header- like windows
#include

#include "draw.h"

// no need to include gl/gl.h here because its already included
// in draw.h, however even if you do include it, there will be
// no problem because of the preprocessor directive contained in
// every header file #ifndef & #define


int WINAPI WinMain(...)
{
// your code goes here
return 0;
}

// endof main.cpp
------------------------------------

Thats how you divide it in linux or windows or for that matter any OS. VC++ is no different. It only has a lot more functionality as an IDE.
Hello from my world

This topic is closed to new replies.

Advertisement