views:

448

answers:

7

Hello. Although I consider myself an experienced programmer (lead programmer of 2 commercially released games), I feel lost now that I have started using C++ for my next project.

Right now I want to create a class to read/encrypt/decrypt JSON-like text-based data files (a custom format I have designed for my needs). Let's say it's called Class1, and it also uses Class2 and Class3. All have separate headers and sources (.h & .cpp). Class1.h contains #include Class2.h and #include Class3.h.

Now, to use Class1 in a project of mine I have to #include Class1.h and manually add Class1.cpp to my project (I am using Visual C++ Express 2008). But AFAIK this won't compile; I also have to manually add Class2.cpp and Class3.cpp to my project, too.

Here are my questions :

1) For every new project that I want to use Class1, I have to manually add all 3 source files. This can become very tedious if I have hundreds of similar classes. Is there a standard way to automate this process?

2) Shouldn't there be a way to automatically add a XY.cpp source file to a project when XY.h is included somewhere in the code?

Thank you for your time. Please forgive me for my ignorance.

A: 

You could create a macro to do it. Visual studio has a more or less fully featured macro scripting environment based around VB - you can script the development environment to add files and write code in response to a keystroke.

1800 INFORMATION
You mean write a nacro to call an existing menu option - seems a bit redundant.
anon
+8  A: 

As far as I know you must include every file you want in your project. VS doesn't do that automatically for you. On the other hand, why don't you simply create a static library so you only need to include that one and its .h? You can pack all of your related classes Class1, Class2, etc. into a single .lib or .dll and include these whenever you need that functionality.

Trap
My thoughts exactly.
Ray Hidayat
With VS adding a reference to a static lib project automatically will link with that library without any other changes.
Dolphin
+2  A: 

I think maybe you are actually asking about libraries. You need a separate project which builds a library from all your .cpp files. Then when you simply add that single library file to your application project.

anon
+3  A: 

The .h files are declarations--okay, I bet you knew that. Adding the .cpp files to your project tells Visual C++ that they need to be compiled and linked into your .exe.

You can avoid needing to add these classes to every project by making them a separate project, a static library or DLL. In that case you'd still need to tell Visual C++ to link against the library--Project Properties->Linker->General->Additional Inputs, I believe (don't have access to Visual Studio to check right now).

Steve McKay
Agreed - putting the files in a nice self-contained, reusable library is definitely a good option.
Jon Cage
A: 

By adding Class1 to your project as long as the compiler can find the include files for Class1, Class2 and Class3 it will compile, it will however fail to link. The standard way of reusing Class1 (and its dependencies across projects is to use libraries). In Visual Studio the easiest way to do this is to include the library project in your solution and place a dependency on it. This way you will avoid having to manually set the inputs in the linker section of the project.

Nic Strong
A: 

As others have pointed out C and C++ treat the interface and implementation separately. The interface is specifed in header files and is used by the compiler. The implementation is specifed in either source files or libraries and is used by the linker. This also means you can have multiple implementations of the same header files and change them at link time (a good trick for unit testing c code or replacing a subsystem).

This can seem a little primitive to programmers used to C# and Java where these lines are blurred.

Basically if you want to share code in C/C++ you have to feed the compiler and feed the linker. If your only platform is Visual C++ you can add pragma's to instruct the linker to link libraries automatically. If you place these in your header files the linker will search for the specified library, but you will still have to tell the linker where to search so I found it rarely worth it, unless the library has multiple versions selected by preprocessor definitions.

see http://msdn.microsoft.com/en-us/library/7f0aews7(VS.71).aspx for details of the pragma.

iain
A: 

A little additional info to the answer of Trap:

If you worry about the .h-File, then just put all the .h-Files on a common directory you include in the search path, than you also don't have to worry about them -- just provide the whole bunch for any project.

Trap was writing about static libraries -- a dynamic library would also work fine, of course!

Juergen