Compiling code on different machine?

M

mlt

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1


in main() {

if(DESKTOP)
{
// do desktop compilation
}
else
{
// do laptop compilation
}

}

I could be nice if there was some system variable that I could test for that
would identify the correct machine.
 
K

Keith Halligan

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1

in main() {

    if(DESKTOP)
    {
        // do desktop compilation
    }
    else
    {
        // do laptop compilation
    }

}

I could be nice if there was some system variable that I could test for that
would identify the correct machine.

Not that I'm aware of.

Perhaps there might be some OS-Specific API's you could use to
determine this, but then your code won't be platform-independent (if
thats what you want).
 
D

DerTopper

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1

in main() {

    if(DESKTOP)
    {
        // do desktop compilation
    }
    else
    {
        // do laptop compilation
    }

}

I could be nice if there was some system variable that I could test for that
would identify the correct machine.

Why don't you simply write two different applications?

Regards,
Stuart
 
M

mlt

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1

in main() {

if(DESKTOP)
{
// do desktop compilation
}
else
{
// do laptop compilation
}

}

I could be nice if there was some system variable that I could test for
that
would identify the correct machine.



Are there compiler options that could be set when using Visual Studio 2009?
 
S

SG

Is there someway in C++ to read a machines ID of somekind?
[...]
I could be nice if there was some system variable that I could test for that
would identify the correct machine.

If by "correct machine" you mean the machine's architecture and/or
operating system this should be covered by a good build system (auto
tools, cmake, ...). In addition you may define your own build
options. Someone who wants to compile your code could specify these
options a la

./configure --labtop-version

or something like that. "cmake" is getting popular these days, btw.


Cheers!
SG
 
I

Ivan

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1

in main() {

    if(DESKTOP)
    {
        // do desktop compilation
    }
    else
    {
        // do laptop compilation
    }

}

I could be nice if there was some system variable that I could test for that
would identify the correct machine.

There are so many macro in C/C++ used for different platform, such as:
all c++ compiler imply the macro __cplusplus__
all c++ compiler following C++ standard imply the macro __STD__.
g++ imply the macro __GNU__
...

There are lots of macros you can use to make your program can run in
most platform. But it didn't provide macro for different machines
(maybe same platform).

For example, your PC and laptop both are X86 platform and under Linux
operating system, this is no need for distinguishing one from other.

If you did want to make it sense. You can do it like below:

#define _desktop_ 1
#define _laptop_ 2

#if !(machine== _desktop_ || machine == _laptop_)
#define machine _desktop_
#endif

int main()
{
#if machine == _desktop_
//code for desktop
#esle
//code for laptop
#endif
}

Then If you can g++, you can specify the macro machine in command
line as:

g++ -Wall -Dmachine=1 -o main main.cpp <<generate code for desktop
version, laptop is similar to it.

Best Regard..
Ivan.
 
M

mlt

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Currently I do:

#define DESKTOP 1

in main() {

if(DESKTOP)
{
// do desktop compilation
}
else
{
// do laptop compilation
}

}

I could be nice if there was some system variable that I could test for
that
would identify the correct machine.

There are so many macro in C/C++ used for different platform, such as:
all c++ compiler imply the macro __cplusplus__
all c++ compiler following C++ standard imply the macro __STD__.
g++ imply the macro __GNU__
...

There are lots of macros you can use to make your program can run in
most platform. But it didn't provide macro for different machines
(maybe same platform).

For example, your PC and laptop both are X86 platform and under Linux
operating system, this is no need for distinguishing one from other.

If you did want to make it sense. You can do it like below:

#define _desktop_ 1
#define _laptop_ 2

#if !(machine== _desktop_ || machine == _laptop_)
#define machine _desktop_
#endif

int main()
{
#if machine == _desktop_
//code for desktop
#esle
//code for laptop
#endif
}

Then If you can g++, you can specify the macro machine in command
line as:

g++ -Wall -Dmachine=1 -o main main.cpp <<generate code for desktop
version, laptop is similar to it.



Nice! You would not by chance know how the corresponding compiler options
are set in Visual studio 2008?
 
J

James Kanze

There's no portable mechanism (AFAIK) guaranteed by the C or
C++ standards, but try gethostbyname(3).

He spoke of compiling, not run time. My makefiles regularly do
this, using $(shell uname -n) to get the host name. This still
doesn't give me any information about the type, however;
basically, I need a different configuration file for every host
I support. On Unix, this is easily handled by means of symbolic
links, but under Windows, I don't know. (And of course, this
supposes standardizing on a common make for all platforms. The
expression I gave above is for GNU make; other makes will likely
have a different syntax, if they support it at all.)
 
J

Jorgen Grahn

Is there someway in C++ to read a machines ID of somekind?

I have some code that should be compiled in a certain way when it is
compiled on a laptop and in another way when its compiled on a desktop.

Compiled on, not run on?
Currently I do:

#define DESKTOP 1

One good general recommendation is to be explicit about what you are
making conditional. Your compiler cannot possibly care if a computer
is a laptop or a desktop or a blade server or a virtual machine
running on some server somewhere in the world or ... -- you must be
after some other property or set of properties: HAS_SLOW_DISK or
HAS_INTERMITTENT_POWER_FAILURES or something.

There's a document by Henry Spencer et al called "ifdefs considered
harmful". Google for it and read it.

/Jorgen
 
A

Alf P. Steinbach

* Jorgen Grahn:
There's a document by Henry Spencer et al called "ifdefs considered
harmful". Google for it and read it.

Thanks, Jorgen.


Cheers,

- Alf
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top