Skip to content


Issue 7678: Added -cflag=ccflags switch. #794

wants to merge 1 commit into from

5 participants


This allows arguments to be passed to the C compiler being used to drive linking.

I've also opened issue 7678 for this. (

I was prompted to make this patch because of issue 5278 remaining unresolved. (

D Programming Language member

Adding more flags makes me nervous. I keep thinking there must be a better solution.

D Programming Language member

Well, -L is already entirely linker-dependent. Does it really matter if we add a switch to pass flags to the C compiler which are compiler-specific? I totally understand your concern, but we already have to special-case anyway, and I very much doubt we can create sensible wrapper options around all supported C compilers on all platforms.

D Programming Language member

The only reason we're using the compiler driver for linking instead of a linker is to select appropriate libc link parameters. Now there are only few C compiler options that affect the selected libc like -static or -fpie. All of those should be mappable to equivalent D compiler options because the linkage model is the same.
So I think it makes more sense to implement a -fPIE switch or interpreting -fPIC for executables as meaning -fPIE.
The implementation would consist of -fPIC + pass -fPIE to gcc + ?.

If that isn't feasible for now I would opt for detecting a default PIE compiler driver or simply hardcode fno-pie

D Programming Language member

I'm not an expert in this so I'll assign to @WalterBright.

@WalterBright WalterBright was assigned
D Programming Language member

I'll close this for now. As stated above we can achieve the particular fPIE fix by different means.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2012
  1. @chadjoan

    Added -cflag=ccflags switch. This allows arguments to be passed to th…

    chadjoan committed
    …e C compiler being used to drive linking.
Showing with 28 additions and 3 deletions.
  1. +6 −1 src/link.c
  2. +12 −2 src/mars.c
  3. +10 −0 src/mars.h
7 src/link.c
@@ -223,7 +223,7 @@ int runLINK()
delete lnkfilename;
return status;
-#elif linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
pid_t childpid;
int i;
int status;
@@ -342,6 +342,11 @@ int runLINK()
argv.push((char *)"-Xlinker");
+ for (size_t i = 0; i < global.params.ccswitches->dim; i++)
+ { char *p = global.params.ccswitches->tdata()[i];
+ argv.push(p);
+ }
/* Add each library, prefixing it with "-l".
* The order of libraries passed is:
14 src/mars.c
@@ -314,8 +314,11 @@ Usage:\n\
files.d D source files\n\
@cmdfile read arguments from cmdfile\n\
-c do not link\n\
- -cov do code coverage analysis\n\
- -D generate documentation\n\
+ -cov do code coverage analysis\n"
+" -cflag=ccflag pass ccflag to the C compiler being used to drive linking\n"
+" -D generate documentation\n\
-Dddocdir write documentation file to docdir directory\n\
-Dffilename write documentation file to filename\n\
-d allow deprecated features\n\
@@ -438,6 +441,7 @@ int tryMain(int argc, char *argv[])
global.params.quiet = 1;
global.params.linkswitches = new Strings();
+ global.params.ccswitches = new Strings();
global.params.libfiles = new Strings();
global.params.objfiles = new Strings();
global.params.ddocfiles = new Strings();
@@ -777,6 +781,12 @@ int tryMain(int argc, char *argv[])
global.params.linkswitches->push(p + 2);
+ else if (memcmp(p + 1, "cflag=", 6) == 0)
+ {
+ global.params.ccswitches->push(p + 1 + 6);
+ }
else if (memcmp(p + 1, "defaultlib=", 11) == 0)
global.params.defaultlibname = p + 1 + 11;
10 src/mars.h
@@ -122,6 +122,15 @@ void unittests();
+// On these systems the C Compiler is used to drive linking.
+// The compiler is chosen using the ${CC} environment variable, but if that is
+// not provided it will default to gcc.
+#if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
struct OutBuffer;
@@ -230,6 +239,7 @@ struct Param
// Linker stuff
Strings *objfiles;
Strings *linkswitches;
+ Strings *ccswitches;
Strings *libfiles;
char *deffile;
char *resfile;
Something went wrong with that request. Please try again.