开发者

Ways not to write function headers twice?

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'开发者_Go百科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


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).


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.


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.


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.


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.


I think it's a good question which is what led me to ask this: 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.


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜