tags:

views:

334

answers:

8

Hi,

I've got a C/C++ question, can I reuse functions across different object files or projects without writing the function headers twice? (one for defining the function and one for declaring it)

I don't know much about C/C++, Delphi and D. I assume that in Delphi or D, you would just write once what arguments a function takes and then you can use the function across diferent projects. And in C you need the function declaration in header files *again??, right?. Is there a good tool that will create header files from C sources? I've got one, but it's not preprocessor-aware and not very strict. And I've had some macro technique that worked rather bad.

I'm looking for ways to program in C/C++ like described here http://www.digitalmars.com/d/1.0/pretod.html

+11  A: 

Imho, generating the headers from the source is a bad idea and is unpractical.

Headers can contain more information that just function names and parameters.

Here are some examples:

  • a C++ header can define an abstract class for which a source file may be unneeded
  • A template can only be defined in a header file
  • Default parameters are only specified in the class definition (thus in the header file)

You usually write your header, then write the implementation in a corresponding source file.

I think doing the other way around is counter-intuitive and doesn't fit with the spirit of C or C++.

The only exception is can see to that is the static functions. A static function only appears in its source file (.cor .cpp) and can't (obviously) be used elsewhere.

While I agree it is often annoying to copy the header definition of a method/function to the source file, you can probably configure your code editor to ease this. I use Vim and a quick script helped me with this a lot. I guess a similar solution exists for most other editors.

Anyway, while this can seem annoying, keep in mind it also gives a greater flexibility. You can distribute your header files (.h, .hpp or whatever) and then transparently change the implementation in source files afterward.

Also, just to mention it, there is no such thing as C/C++: there is C and there is C++; those are different languages (which indeed share much, but still).

ereOn
Good point about extra information in headers - might want to mention default parameters for member functions can only be specified in the header, and most template code lives in headers only.
AshleysBrain
@AshleysBrain: Nice examples indeed. Added them to my answer. Thanks !
ereOn
A: 

Considering you have declared some functions and wrote their implementation you will have a .c/cpp file and a header .h file.

What you must do in order to use those functions:

  1. Create a library (DLL/so or static library .a/.lib - for now I recommend static library for the ease of use) from the files were the implementation resides
  2. Use the header file (#include it) (you don't need to rewrite the header file again) in your programs to obtain the function definitions and link with your library from step 1.

Though >this< is an example for Visual Studio it makes perfect sense for other development environments also.

Iulian Şerbănoiu
Actually you do not have to create a library. You could also just link in the object file (just for completeness).
Space_C0wb0y
@Space_C0wb0y True
Iulian Şerbănoiu
A: 

This seems like a rudimentary question, so assuming I have not mis-read, Here is a basic example of re-use, to answer your first question:

#include "stdio.h"

int main( int c, char ** argv ){
    puts( "Hello world" );
}

Explanation: 1. stdio.h is a C header file containing (among others) the definition of a function called puts(). 2. in main, puts() is called, from the included definition.

Some compilers (including gcc I think ) have an option to generate headers.

jdu.sg
A: 

There is always very much confusion about headers and source-files in C++. The links I provided should help to clear that up a little.

If you are in the situation that you want to extract headers from source-file, then you probably went about it the wrong way. Usually you first declare your function in a header-file, and then provide an implementation (definition) for it in a source-file. If your function is actually a method of a class, you can also provide the definition in header file.

Technically, a header file is just a bunch of text that is actually inserted into the source file by the preprocessor:

#include <vector>

tells the preprocessor to insert contents of the file vector at the exact place where the #include appears. This really just text-replacement. So, header-files are not some kind of special language construct. They contain normal code. But by putting that code into a separate file, you can easily include it in other files using the preprocessor.

Space_C0wb0y
+1 Probably one of the best answers
nico
-1 It's an explanation of how header/source files work, not an answer to the question.
Joe Gauterin
In my opinion the way the question was asked reveals that the OP does not understand the way headers work. If he did, he would be able to answer his question himself. Any answer that solves his problem, but doesn't help him understand it in the first place, is really not helping very much. I think I have provided all the information needed to make him solve the problem by himself.
Space_C0wb0y
A: 

I think it's a good question which is what led me to ask this: http://stackoverflow.com/questions/2607845/visual-studio-automatically-update-c-cpp-header-file-when-the-other-is-changed

There are some refactoring tools mentioned but unfortunately I don't think there's a perfect solution; you simply have to write your function signatures twice. The exception is when you are writing your implementations inline, but there are reasons why you can't or shouldn't always do this.

Jon
+1  A: 

It seems to me that you don't really need/want to auto-generate headers from source; you want to be able to write a single file and have a tool that can intelligently split that into a header file and a source file.

Unfortunately, I'm not aware of any such tool. It's certainly possible to write one - but you'd need a given a C++ front end. You could try writing something using clang - but it would be a significant amount of work.

Joe Gauterin
A: 

You might be interested in Lazy C++. However, you should do a few projects the old-fashioned way (with separate header and source files) before attempting to use this tool. I considered using it myself, but then figured I would always be accidentally editing the generated files instead of the lzz file.

Emile Cormier
A: 

You could just put all the definitions in the header file...

This goes against common practice, but is not unheard of.

BlueRaja - Danny Pflughoeft