Notice

The forum is in read only mode.

  • Page:
  • 1

TOPIC: How to combine C++ header and source into unit?

How to combine C++ header and source into unit? 6 years 5 months ago #1576

Hello,

I'm new to C++Builder and I'm working with C++Builder XE6.

Could anyone explain me (or give a link to some resource), how to combine existing C++ source and header files into "unit", so that e.g. I can use "CTRL + F6" to switch between them) when the files are already exist?

Thanks in advance.

How to combine C++ header and source into unit? 6 years 5 months ago #1586

Hi Dmitrii,

All you need to do is add the same "header guard" pattern we use in generated unit files.

Put this in your header .h file (replace test with your file name):
#ifndef testH
#define testH
// source code
#endif

and put this in your source .cpp file

#pragma hdrstop
#include "test.h"
//source code
#pragma package(smart_init)
The following user(s) said Thank You: Dmitrii

How to combine C++ header and source into unit? 6 years 5 months ago #1590

Hello, J T.

Thanks for your answer. Indeed this works. Additional note: the casing of guard macro is not important, i.e. one can use either "testH" or "TESTH" or "testh" in the example above (but no underscores etc.)

Though, the hole thing is not very good for me, because I have some source base, which I have to adapt to C++Builder and this source base has different convention for naming guards. So I either have to change the guard names for all header files or to give up the idea of using units (and use separate header and source files included to the project, which is not too convenient as I cannot switch between them easily).

After all, I don't understand, why guard name influence if the header is detected as part of the unit? Guard name is the only property of the header itself and does not connect it to the source file in any way. So, why not just check the file name of the header? Or why not to make it possible to set up the unit manually? But probably I miss some point.

Anyway, thanks again for the answer. At least now I know why it did not work for me from the very beginning.
  • Page:
  • 1
Moderators: April Anne