compile order ms vs c++

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

compile order ms vs c++

Post by Desperado »

Hi everyone.

Does anybody know how to change the compile-order in VStudio c++ ?
(it's a jungle... :( )

The reason is, that i have a header which contains constants.
My main-modul gets the definitions while all other moduls just getting
a declaration.

All works fine, but when i want to use a switch-command, suddenly the
error occurs "unknown constant" (something like this).

The reason for this is that at compile time the switch-statement needs the definition (declaration is not enough).
So all moduls compiled before my main-modul can use switch, all others
i have to use if..else - statement.

workarounds i dont want:

- using if..else if..
- function pointers
- and of course not giving each modul the same data in a static way...
(hope you know what i mean with the last point, i have to train my english :-) )

so simple, clean solution is to change the compile order , but how ?

(perhaps i am just to blind,stupid or something else, i searched for hours
now and cant find out how to do)
thx a lot
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: compile order ms vs c++

Post by bob »

Desperado wrote:Hi everyone.

Does anybody know how to change the compile-order in VStudio c++ ?
(it's a jungle... :( )

The reason is, that i have a header which contains constants.
My main-modul gets the definitions while all other moduls just getting
a declaration.

All works fine, but when i want to use a switch-command, suddenly the
error occurs "unknown constant" (something like this).

The reason for this is that at compile time the switch-statement needs the definition (declaration is not enough).
So all moduls compiled before my main-modul can use switch, all others
i have to use if..else - statement.

workarounds i dont want:

- using if..else if..
- function pointers
- and of course not giving each modul the same data in a static way...
(hope you know what i mean with the last point, i have to train my english :-) )

so simple, clean solution is to change the compile order , but how ?

(perhaps i am just to blind,stupid or something else, i searched for hours
now and cant find out how to do)
thx a lot
You just need to #include that header file at the front. Although I am not sure what you mean by "constants" which could be #defines or something else... But whatever it is, it has to be included before anything inside can be referenced.
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: compile order ms vs c++

Post by Desperado »

At first thx for reply, but i have to mention that the data is declared already, it is used ! in the scope where it is declared, but
"switch statement doesnt work". Otherwise nothing would work without knowing the constants.

So here is little pseudo-code of what the matter is.

Code: Select all


//===============main-modul(desperado.cpp)=======///

#define  DESPERADO_BIB
#define  DESPEARDO_H
#include "desperado.h"

//----------
this is the modul i want to compile(link) first, it should include all public data in its header...
//----------

void main(void)
{
 ...
}

more code...


//===============main-header(desperado.h)=======///

#ifdef DESPERADO_BIB

  #define PIECE_H
  #include "piece.h"

#endif

#ifdef DESPERADO_H

 ...data declaration/definition...

#endif


//===============common header(piece.h)=======///

#ifndef PIECE_DEC

#define PIECE_DEC
extern const UI_08 wp; (modulx just knows that somewhere it is defined, and it will be used, but not for the switch command)
...

#endif

#ifdef PIECE_H

 extern const UI_08 wp = 1; (compiler needs "extern" to export constant)
 extern const UI_08 wn = 2; (so the desperado-modul now has declaration and definition)
 ...

#endif

//===============modul-X (modulx.h)=======///

#ifdef MODULX_BIB

  #include "piece.h" (but now without defining PIECE_H)

#endif

CASE1: modulx compiled AFTER desperado.cpp

Code: Select all


if(p == wp) //statement is ok

switch(p)    //statement is ok
{
 case wp: break;
};

CASE2: modulx compiled BEFORE desperado.cpp

Code: Select all


if(p == wp) //statement is ok

switch(p)    //ERROR !!! (requieres definition, but it is "only" declared...)
{
 case wp: break;
};
i hope it gets more clear now what i mean. Of course having a header in front solves the problem, i get it in front if desperado.cpp is compiled first! :-) or not ? So ms-comiles in a...z order i think and i dont want to push the "define PIECE_H" in "a"ttack header :-). i just want to control the
compile-order, but i dont know how...(the thing with the switch-statement isnt a specific microsoft-compiler problem, it is general. Perhaps not for all c++-compilers)
User avatar
Onno Garms
Posts: 224
Joined: Mon Mar 12, 2007 7:31 pm
Location: Bonn, Germany

Re: compile order ms vs c++

Post by Onno Garms »

Hi Michael,

I still don't get your code structure completely. In which file are CASE 1 and CASE 2 code fragments located and which #includes and #defines are done by that file?

The order in which you compile different cpp-files should not matter. If VStudio remembers definitions from previously compiled modules that is an obscure feature. You should not try to exploit that by affecting the compilation order.

Case statements must know at least the definition of constants. I am a bit surprised that they work with constants at all, I would have expected that they only work with hardcoded integers and enums.

The standard way to solve solve your problems might be to rewrite piece.h as follows:

Code: Select all

#ifndef PIECE_DEC
#define PIECE_DEC

enum { wp=1, wn=2 };

#endif
Note that the macro PIECE_H is no longer used.
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: compile order ms vs c++

Post by Desperado »

hi Onno,

enum cannot get the "extern" statement...

So it would be necessary to put the enum-statement in each header, or
to have one header with the enum(constants), which then is included by each modul(like Robert mentioned).

now for my explanation enum and constants are handled equal...

That would mean, that each modul creates its own instances of the
constants(enums), so 5 moduls 5xwp= 5byte :-) (not much,but not necessary !, it is a principal thing now).

dont forget that the declared constants can be used like in the if(p==wp) statement(but not in the switch). So in the sense of programming all is
done correctly.

for constants the compile order is important, because the compiler directly put the value of the constant in the assembler("assembly...ah you know...). (So a constant is at least a hardcoded integer)

Like the way i handle it, there is only one defininition 1byte. all other moduls use this definition. And it is not the problem to get it to run,
it is the thing, that i want 1 definition, 1 memory place and not 5 or X, and
can keep it as standalone header.(like roberts approach, but without memory loss)
so i want to have a header with all global data once included in one modul (mainmodul), if this is compiled
first, each other modul gets the definition by declartion of the constant.
(Roberts and your version multiplies the data)
definitions.

So i dont have to mix it with modul specific data, and i dont waste memory ... thx for reply

the case1+case2 code fragments should just let you know, that in any
modul the behaviour of using the constant wp can differ.
(Not in general, but...the switch...)
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: compile order ms vs c++

Post by mcostalba »

Desperado wrote:hi Onno,

enum cannot get the "extern" statement...
Of course ! they do NOT allocate anything!
Desperado wrote:
That would mean, that each modul creates its own instances of the
constants(enums), so 5 moduls 5xwp= 5byte :-) (not much,but not necessary !, it is a principal thing now).
IMHO the only principal thing that should metter would be not to mess up the code with dubious preprocessors tricks.

And in any case, again, the enum definition DOES NOT allocate anything, only a variable on an enum type does so your math seems wrong to me.
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: compile order ms vs c++

Post by Desperado »

Hi marco,

ok enum doesnt allocate anything, then it is just a declaration (the same
thing i do).

But if you look at the pseudo-code there arent any "tricks".
(2 moduls (modulx,desperado) including "piece.h"), what "doubious" tricks? (pls dont get ugly...)

it is just definition of constants and declaration of them, and that happens simply in the belonging headerfiles, not more not less.

And what happens if variable on enum type is used, mem will be allocated (on local scope?), or not ?

At the end i am only interested, of how the compile ordering can be changed in visual studio ?

So (sorry,pls explain), when i give each modul the enum-type and then use it by a variable each modul will create and allocate its own variable/mem ?
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: compile order ms vs c++

Post by Desperado »

once again, more simply...

Code: Select all


//----------------------------------------------------------
MODUL1:

//============DESPERADO.H=======

#define PIECE_H

#include "piece.h"

//==============================

//----------------------------------------------------------

MODUL2:

//===========ANY_OTHER_MODUL.H==

#include "piece.h"

//==============================

//----------------------------------------------------------

STANDALONE HEADER:

//===========PIECE.H============

#ifndef PIECE_DEC

	#define PIECE_DEC

	extern const UI_08 c_val; //no definition
#endif



#ifdef PIECE_H

	extern const UI_08 c_val = 0; //definition

#endif

//==============================

//----------------------------------------------------------
so pls marco, let me know the dubious tricks here (i simplified a little bit) ?
User avatar
Onno Garms
Posts: 224
Joined: Mon Mar 12, 2007 7:31 pm
Location: Bonn, Germany

Re: compile order ms vs c++

Post by Onno Garms »

Desperado wrote:enum cannot get the "extern" statement...
There is no need for that.
So it would be necessary to put the enum-statement in each header, or
to have one header with the enum(constants), which then is included by each modul(like Robert mentioned).
The first is bad style, use the second.
now for my explanation enum and constants are handled equal...
Not quite. consts still have an address. enums haven't.
That would mean, that each modul creates its own instances of the
constants(enums), so 5 moduls 5xwp= 5byte :-) (not much,but not necessary !, it is a principal thing now).
There are no instances of enums. They are just like #defines. With constants you should in deed avoid to have instances in every module.
dont forget that the declared constants can be used like in the if(p==wp) statement(but not in the switch). So in the sense of programming all is
done correctly.
Sure, if (p==wp) only needs to know that there is a varialbe wp with some value. The switch statement does not accept variable labels. The label values must be known at compile time.
for constants the compile order is important, because the compiler directly put the value of the constant in the assembler. (So a constant is at least a hardcoded integer)
Normally the compiler should forget everything after it compiled one cpp file. Also it should be possible to compile several cpp files in parallel. So the compile order definitely should not matter.

As I understand your postings, the M$ compiler remembers values of constants it has seen when compiling previous modules. (I have not checked that.) If you rely on that, you might bind yourself to the M$ compiler or even your specific version of the M$ compiler.

What happens / would you wish to happen if you build without a complete rebuild? Possibly VStudio sees that you only need to recompile one module, but not the main module.

In deed the compiler should be able to see the definition from everywhere to be able to put directly its value in the assembly. But compile order is not the way to achieve that.
i want 1 definition, 1 memory place and not 5 or X, and
can keep it as standalone header.
With enums you have one definition, 0 memory places, and a standalone header. Btw, a preprocessor #define will achieve the same (but might occasionally lead to obscure compiler errors when you try to define a local variable wp).
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: compile order ms vs c++

Post by Desperado »

hi again onno, (thx for your interest)

i agree with:
===========

- your better style statement
- that constants arent the same as enums (i just put them together because both dont help me, but of course of different reasons)
- the compile order should not matter

i dont agree with:
=============

- if(p==wp) -> wp isnt a variable it is a constant (normally here the compiler should put in the value, and if so this statement also dont has to work)
- the compile order "should" not matter, but it does.
- does extern declared data is really forgotten after compiled a xxx.cpp?

And one more question, now a discussion is running on that topic.
Anywhere at any time there will be memory allocated for a enum-variable
or not ?
Each standard datatype has maximal modulscope except for "extern" declared data.(so there would be different allocations for different moduls ?)

i simplified the pseudocode in the post before, perhaps you want to have a look...