Is it possible to change the environment variable through a makefile?

I would like to use the MALLOC_PERTURB_ environment variables that can change the memory allocation parameters (man 3 mallopt). However, I would like to control the distribution parameters at the application level, not at the entire system level. Ideally if I could control them through the project makefile. I tried to change the mentioned variables via makefile but with no success.

For testing purposes, I created this test.c file:

#include<stdlib.h>
#include<stdio.h>

#define N 50

int main()
{
    char *chars;
    int i;

    if (NULL == (chars = malloc(N * sizeof(*chars))))
        return EXIT_FAILURE;

    free(chars);

    for (i = 0; i < N; ++i)
        printf("%c", chars[i]);
    printf("\n");

    return EXIT_SUCCESS;
}

      

Yes, I know I'm reading from freed memory, but it's all about using MALLOC_PERTURB _.

Expected Result: 50 ASCII characters of value MALLOC_PERTURB _.

close enough:

$ export MALLOC_PERTURB_=97
$ gcc test.c -o test
$ ./test
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  


$ export MALLOC_PERTURB_=105
$ gcc test.c -o test
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii  

      

Then I tried to enclose the compilation in a makefile, with no success.

Variable export (as suggested here)

Makefile

all:
    export MALLOC_PERTURB_=110
    gcc test.c -o test

      

Result (I expected the letter "n" for 110)

$ export MALLOC_PERTURB_=105
$ make
export MALLOC_PERTURB_=110
gcc test.c -o test 
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii  

      

Calling recursively (as suggested here)

Makefile

all:
    MALLOC_PERTURB_=110
    $(MAKE) rec

rec:
    gcc test.c -o test

      

Result (I expected the letter "n" for 110)

$ export MALLOC_PERTURB_=105
$ make
MALLOC_PERTURB_=110
make rec
make[1]: Entering directory '~/test'
gcc test.c -o test
make[1]: Leaving directory '~/test'
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii  

      

I tried to find any inspiration from the github makefiles that included MALLOC_PERTURB_, but they are too complex for me to figure out. Some examples: (1) , (2) , (3)

Some technical information:

Linux 4.0.1-1-ARCH x86_64
gcc version 4.9.2 20150304 (prerelease) (GCC)
GNU Make 4.1

      

+3


source to share


3 answers


Browning, setting the environment variable in the makefile is easy and you've done it, but that's not what you want. This makes it assume this value at compile time. But you want it to change malloc behavior when the program starts.

Reading the man page you linked to confirms that you need to set an environment variable in the environment your program is running in. Also, after reading the comments, it has already been mentioned that the makefiles that set the environment variable only do so for the test program, which they actually execute as part of the build.



You don't want this in the makefile. You want it to be installed during program startup, which is what other comments and answers tell you how to do. Sorry for your answer, but I need more space to clarify this, plus this is the answer for you. You already know how to do this. You just didn't know what it was, exactly.

+2


source


In Bash, you can specify environment variables before the command, for example

MALLOC_PERTURB_=105 gcc test.c -o test

      

or



MALLOC_PERTURB_=105 ./test

      

(I believe only this launch should have this variable, not compilation.)

Since you are on Linux, there is a good chance Make will use Bash as its shell and you can put the line above in your file.

+5


source


You can set the value "MALLOC_PERTURB_" at compile time by passing a M_PERTURB

function parameter mallopt()

(glibc 2.4 or later). See http://man7.org/linux/man-pages/man3/mallopt.3.html for details .

So, if you want your program to include setting the "MALLOC_PERTURB_" environment variable at compile time, then something like this should work:

#include<stdlib.h>
#include<stdio.h>

#include <malloc.h> // for mallopt() and M_PERTURB

#define N 50

int main()
{
    char *chars;
    int i;

#if MALLOC_PERTURB_
    mallopt( M_PERTURB, MALLOC_PERTURB_);
#endif

    if (NULL == (chars = malloc(N * sizeof(*chars))))
        return EXIT_FAILURE;

    free(chars);

    for (i = 0; i < N; ++i)
        printf("%c", chars[i]);
    printf("\n");

    return EXIT_SUCCESS;
}

      

Then make your makefile the value MALLOC_PERTURB_

as the definition of a macro on the compiler command line (or whatever mechanism you want to use):

gcc -DMALLOC_PERTURB_=97 test.c -o test

      

+1


source







All Articles