Thread: Set variable from makefile?

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Set variable from makefile?

    I'm using autotools (for the first time) to generate a makefile and all the other things that the GNU autotools do. Something I wrote is supposed to be included in an upcoming release of a major linux distro so I need to get this stuff right.

    Anyway, the program needs to makes use of some external files (eg, images) that I figure will go in INSTALLDIR/share/myapp/. Up until now, the exact location of the files was always known prior to compile, so I just used this:
    Code:
    #define FILEPATH /usr/local/share/myapp
    Since INSTALLDIR won't be determined until compile time (by "configure"), how can I adjust the source code so that FILEPATH will be correct?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use -D to define things from the command line. (Where in this case, "command line" = "makefile".)

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You can use -D to define things from the command line. (Where in this case, "command line" = "makefile".)
    Hey isn't that neat!

    Unfortunately I cannot get it to function for a string literal -- the compiler seems to regard whatever I assign to the "macro" as a variable name and not a value, eg. given
    Code:
    #include <string.h>
    
    int main() {
    	char ptr[64];
    	strcpy(ptr,X);
    	return 0;
    }
    gcc -D X=this test.c (or -D X="this" or -DX='this')...
    test.c: In function ‘main’:
    test.c:5: error: ‘this’ undeclared (first use in this function)

    Notice that it's this which is undeclared and not X.
    Last edited by MK27; 01-30-2009 at 02:18 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You probably need escaped double quotes: -DX=\"this\"

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by root4 View Post
    You probably need escaped double quotes: -DX=\"this\"
    Yay, root4!

    For posterity, I added this to Makefile.am in src/
    Code:
    myapp_CFLAGS = -D FILEPATH=\"$(datadir)/@PACKAGE@\"
    $(datadir) is already part of the automake deal and defaults to /usr/local/share
    Last edited by MK27; 01-30-2009 at 03:23 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM