开发者

mudflap without "export"

开发者 https://www.devze.com 2023-02-27 09:41 出处:网络
Is it possible to set the variable MUDFLAP_OPTIONS inside the C program rather than having to export it from bash ?

Is it possible to set the variable MUDFLAP_OPTIONS inside the C program rather than having to export it from bash ?

myuser@linux:~$ export MUDFLAP_OPTIONS="-mode-check -viol-abort -internal-checking -heur-stack-bound -heur-start-end -verbose-violations -crumple-zone=32"
myuser@linux:~$ gcc -fmudflap -funwind-tables -lmudflap -rdynamic myprogram.c

I tried this but it does not work:

static char *var = "MUDFLAP_OPTIONS=-mode-check -viol-abort -internal-checking -heur-stack-bound -heur-start-end -verbose-violations -crumple-zone=32";
putenv(va开发者_开发知识库r);


You don't have to export to anything but the program you want to start. Like this:

VARNAME=value ./program

So for your case:

MUDFLAP_OPTIONS="-mode-check -viol-abort -internal-checking -heur-stack-bound -heur-start-end -verbose-violations -crumple-zone=32" ./myprogram


Mudflap is probably reading the options before main is called, so you won't have a chance to write the options to the environment before it reads them.

Why not just write a wrapper script, that sets the options and invokes your executable?

#!/bin/sh
export MUDFLAP_OPTIONS="-mode-check -viol-abort -internal-checking -heur-stack-bound -heur-start-end -verbose-violations -crumple-zone=32"
./my-executable


This is why Makefiles exist. gcc will not execute your program for you, nor will it get any environment variables from your code. Instead you need to set this kind of stuff inside of a Makefile, like this:

export MUDFLAP_OPTIONS="-mode-check -viol-abort -internal-checking -heur-stack-bound -heur-start-end -verbose-violations -crumple-zone=32"

all:
    gcc -fmudflap -funwind-tables -lmudflap -rdynamic myprogram.c

Save this as Makefile and simply type make to compile your program. You'll never need to remember to export that variable again.


You can add Mudflap related additional flags to your make file as below

MUDFLAP_OPTIONS+=-fmudflap -fmudflapth -funwind-tables 

Then link it with -lmudflapth -rdynamic as below

LDFLAGS+=-lmudflapth -rdynamic

Note: the flag "-fmudflapth" is required only if your code is a multi threaded one. If not you can avoid that flag and while linking use "-lmudflap" instead of "-lmudflapth"

For mudflap help use MUDFLAP_OPTIONS="-help" ./myexecutablefile

0

精彩评论

暂无评论...
验证码 换一张
取 消