-4

In a header file I have a parameter that specifies the name of a control file:

#define CTLFILE "server.ini"

This works fine. But now I want something like this:

If I am on the server

#define CTLFILE "server.ini"

else if I am on the client

#define CTLFILE "client.ini"

How can I implement this?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Dax
  • 438
  • 1
  • 8
  • 29
  • Do you want to do it runtime or at compile time? – LPs Jan 12 '17 at 09:41
  • To add to @LPs, it seems unclear, can you define the _enviroment_ you're referring to? – Sourav Ghosh Jan 12 '17 at 09:43
  • You can use command line parameters to tell your program whether it is on a server or a client PC. If you want to read some environment variables the solution depends on the used OS. The solution will probably also depend on whether you want to use c or c++ which is it? – MikeMB Jan 12 '17 at 09:46
  • @LPs, I meant Server or Client – Dax Jan 12 '17 at 09:50
  • Well, ok that is clear. The question is: do you want a single executable that change its behavior based on the environment that is executing it? Or, as @SouravGhosh suggested, can compile 2 different executables? – LPs Jan 12 '17 at 09:53
  • Also how do you determine if you are on the server or on the client? What mechanism is used for that decision? – UnholySheep Jan 12 '17 at 09:56
  • @LPs, I don't want to compile to times. It is not possible – Dax Jan 12 '17 at 10:00
  • @UnholySheep, I don't know now – Dax Jan 12 '17 at 10:01
  • Faster tip is to pass file in command line when program is launched. As @MikeMB already suggested. Otherwise you should configure your environment and read this configuration with your code. – LPs Jan 12 '17 at 10:02
  • LPS, yes, that is what I also think – Dax Jan 12 '17 at 10:03
  • LPs, how can I diffrenciate between server and client? what shall I check? – Dax Jan 12 '17 at 10:05
  • Simply using [argc argc](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean)? – LPs Jan 12 '17 at 10:06
  • 1
    @LPs You meant `argc, argv`? – Sourav Ghosh Jan 12 '17 at 10:26
  • @SouravGhosh Yes ;) – LPs Jan 12 '17 at 10:53
  • __Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.__ – Sourav Ghosh Jan 12 '17 at 11:34

3 Answers3

1

You can pass option when launch your program:

For example try to call the following program passing server or client:

#include <stdio.h>
#include <string.h>

#define SERVER_FILE "server.ini"
#define CLIENT_FILE "client.ini"

int main (int argc, char *argv[])
{

    if (argc<2)
    {
        fprintf(stderr, "You mast pass type of envirnment\n!");
        return 1;
    }

    if (strcmp(argv[1], "server") == 0)
    {
        printf ("File selected: %s\n", SERVER_FILE);
    }
    else if (strcmp(argv[1], "client") == 0)
    {
        printf ("File selected: %s\n", CLIENT_FILE);
    }
    else
    {
        fprintf(stderr, "Not supported environment %s", argv[0]);
        return 1;
    }

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
0

You can use conditional compilation by using #ifdef....#endif pair.

For example, in the code, put it like

#ifdef SERVERSIDE
#define CTLFILE "server.ini"
#else
#define CTLFILE "client.ini"
#endif

Then, while compiling, pass -DSERVERSIDE option to the compiler (reference: gcc).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You can't do this that way, because #define and all #something are preprocessor instruction. That mean that after compilation, all #something are "gone", so you can't execute the same program differently with preprocessor instruction.

Many choice :

*) You compile twice the program with different #define CTLFILE. *) You develop something like a configuration file in order to configure the execution of your program.

This will need extra development since you will have to dynamicly change string. It's up to you.

*) Just test for the existence of "server.ini" or "client.ini" file. Work if the two file don't exist at the same time.

Tom's
  • 2,448
  • 10
  • 22
  • I don't want to compile the program twice. Is there another way? I am not a c++ programmer , but I have to fix this to go further :) – Dax Jan 12 '17 at 09:58