Compile without header

A

ajlu

Hi all,

Is it possible to compile a c++ program without header, if possible
please let me know in detail...

***ajlu***
 
B

Bo Persson

ajlu said:
Hi all,

Is it possible to compile a c++ program without header, if possible
please let me know in detail...

***ajlu***

It is possible only if the entire program resides in one file. If you want
to refer to anything outside your own code, you have to include the
appropriate headers.


Bo Persson
 
K

Kohn Emil Dan

It is possible only if the entire program resides in one file.

This is not true. For example:

// a.cpp

extern int get_return_code(void);

int main()
{
int rc = get_return_code();
return rc;
}
//---------------



//b.cpp

int get_return_code(void)
{
return 0;
}
//---------------------------------------------------
If you want
to refer to anything outside your own code, you have to include the
appropriate headers.


Now I agree that programming the way I showed above is extremely bad
practice, and should be avoided by all means. The correct solution is of
course using header files.


Emil
 
B

Bo Persson

Kohn said:
This is not true. For example:

// a.cpp

extern int get_return_code(void);

int main()
{
int rc = get_return_code();
return rc;
}
//---------------



//b.cpp

int get_return_code(void)
{
return 0;
}
//---------------------------------------------------



Now I agree that programming the way I showed above is extremely bad
practice, and should be avoided by all means. The correct solution
is of course using header files.

I first thought I should mention that you can do the preprocessor's job by
cutting and pasting the headers into your source file.

I then decided not to tell the OP about that. :)


Bo Persson
 
S

Salt_Peter

ajlu said:
Hi all,

Is it possible to compile a c++ program without header, if possible
please let me know in detail...

***ajlu***

Its impossible to carry out a compilation *with* a header. Only source
files can be compiled.
You are aware that the preprocessor substitutes #includes into your
source *before* compilation starts (with respect of the include guards
in those headers). In fact, a compiler doesn't know what a header file
is.

You write (ignoring include guards):
// a.hpp
class A { };
// test.cpp
#include <a.hpp>
int main() { A a; }

Compiler sees:
// test.cpp
class A { };
int main() { A a; }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top