Results 1 to 6 of 6

Thread: Need help with compiling C++ program

  1. #1
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Need help with compiling C++ program

    I have written a C++ program that first generates a random serial number into a file and then reads the serial number from the file and use it in some other logic. Now I want to compile this into a binary file that the secret code will be available within the program as a constant value. Is it possible to set variables or macro's at compile time with values external to the program? Thanks in advance.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Need help with compiling C++ program

    You can define macros at compile time with the -D option to g++:

    Code:
    g++ -o myprogram -DANSWER=42 myprogram.cc
    This is the same as doing a #define in your program:

    Code:
    #define ANSWER 42
    The value on the command line doesn't have to be a constant, it could be the output of a program:

    Code:
    g++ -o myprogram -DDATE="\"$(date)\"" myprogram.cc
    The quoting gets a bit messy with strings.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Need help with compiling C++ program

    You can define macros at compile time with the -D option to g++:

    Code:
    g++ -o myprogram -DANSWER=42 myprogram.cc
    This is the same as doing a #define in your program:

    Code:
    #define ANSWER 42
    The value on the command line doesn't have to be a constant, it could be the output of a program:

    Code:
    g++ -o myprogram -DDATE="\"$(date)\"" myprogram.cc
    The quoting gets a bit messy with strings.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  4. #4
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help with compiling C++ program

    Quote Originally Posted by r-senior View Post
    You can define macros at compile time with the -D option to g++:

    Code:
    g++ -o myprogram -DANSWER=42 myprogram.cc
    This is the same as doing a #define in your program:

    Code:
    #define ANSWER 42
    The value on the command line doesn't have to be a constant, it could be the output of a program:

    Code:
    g++ -o myprogram -DDATE="\"$(date)\"" myprogram.cc
    The quoting gets a bit messy with strings.
    Thanks, trying that way. However, multiple words causing a bit trouble.

  5. #5
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Need help with compiling C++ program

    Quote Originally Posted by zobayer1 View Post
    However, multiple words causing a bit trouble.
    Try e.g

    Code:
    -DSOMEVAR='"Some words here"' [...]
    To set a macro SOMEVAR to a string "Some words here".

  6. #6
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Need help with compiling C++ program

    Quote Originally Posted by MG&TL View Post
    Try e.g

    Code:
    -DSOMEVAR='"Some words here"' [...]
    To set a macro SOMEVAR to a string "Some words here".
    Thanks, it's much simpler, I was thinking about putting spaces after some escape characters, but looks like single quotes does it.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •