GCC Debug Flags SCons - c++

I've been using qmake up till now and it passed the correct flags for GCC to generate debug information when compiling without me ever having to worry about passing the correct flags. Now I have to use SCons due to the needs of the project and I've not yet still gotten used to it. How do I pass debug flags to GCC using SCons so that GCC can generate debug information for GDB? (By the way, I already tried '-g', but it didn't work.)
Thanks in advance to anyone who replies!

To pass flags to the compiler you can set one of the following SCons Construction Variables.
CPPFLAGS
User-specified C preprocessor options
CFLAGS
General options that are passed to the C compiler (C only; not C++).
CCFLAGS
General options that are passed to the C and C++ compilers
CXXFLAGS
General options that are passed to the C++ compiler
For example, if you are only using C++, you can do the following:
env = Environment()
env.Append(CXXFLAGS='-g')
env.Program(target='yourTarget', source='yourSource.cc')

You can add options to the SCons command line using AddOption. For example, your SConstruct could look like this:
AddOption('--dbg', action='append_const', dest='cflags', const='-g')
AddOption('--opt', action='append_const', dest='cflags', const='-O2')
env = Environment()
env.MergeFlags(GetOption('cflags'))
SConscript('SConscript', exports={'env':env})
Your SConscript wouldn't change:
Import('env')
env.Program('foo.c')
You control the compilation with command-line parameters to scons:
$ scons -Q
gcc -o foo.o -c foo.c
gcc -o foo foo.o
$ scons -Q --dbg
gcc -o foo.o -c -g foo.c
gcc -o foo foo.o
$ scons -Q --opt
gcc -o foo.o -c -O2 foo.c
gcc -o foo foo.o
Reference:
http://www.scons.org/doc/HTML/scons-user/apd.html
https://docs.python.org/2/library/optparse.html

Related

g++ creates shared object with dynamic dependency even though I gave it a static archive in the inputs

I have a source file, mything.cpp, and a library that was provided to me as notmine.h and notmine.a.
I need to produce a shared object that has all my stuff from mything.cpp and all the stuff from somelib.a. Here is what I did on the command line:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o notmine.a
However, when I look at the final mything.so using ldd I see that it has a dependency on libnotmine.so, and when I check nm, I see that all the symbols that should have been supplied by notmine.a are undefined.
What am I doing wrong?
More details: notmine.a is actually liblua.a that I built locally. I think g++ might be getting confused because there is a liblua.so in the system directories
Finally figured it out. There are two options. The simpler is to use:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -L. -l:notmine.a
Alternatively, you can tell the linker you want to treat the .a as a bunch of object files with
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -Wl,--whole_archive notmine.a -Wl,--no-whole-archive
The --Wl,-no-whole-archive is to prevent that flag from messing up the other steps the linker does with the system libraries

MinGW, how to avoid linking statically full libstdc++

I am using mingw 64 bit with cygwin.
I know that if I compile using
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp
the output .exe does not run unless the library path to libstdc++ and other libraries is specified in the Path environment variable.
An alternative is to link statically
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread
Since I want a single .exe that I can easily copy on different machines, the second solution is better for me. My only problem is that, since I link statically, even for a simple helloworld program, the size of the executable rises to more than 10 Mb. So my question is: is it possible to link statically only the library parts that are actually used by the program?
The binutils linker on Windows, ld, does not support the --gc-sections argument properly, which in combination with compiler flags -ffunction-sections and -fdata-sections, allow the linker to throw away blocks of unused code.
You are straight out of luck. The only thing you can do is the usual: strip the executable (by running the strip command on it after it is linked) and compile your code optimising for size with -Os.
Keeping in mind these options do not work on Windows (which for the purpose of this answer, includes the Cygwin platform), this is generally how you can do this:
g++ -c -Os -ffunction-sections -fdata-sections some_file.cpp -o some_file.o
g++ -c -Os -ffunction-sections -fdata-sections main.cpp -o main.o
g++ -Wl,--gc-sections main.o some_file.p -o my_executable

Translate CL.exe to mingw command

I have this CL.exe command
cl -Fesample.dll -Oi -LD -D NO_TRACE=1 -MT sample.cpp sample_dll.def
and i would like to compile it on Linux with MinGW and this is what i have so far
i586-mingw32msvc-gcc -Fesample.dll -shared -D NO_TRACE=1 sample.cpp sample_dll.def
Tracing down the cl command options
-Oi is optimisation -> not important at this point
-LD is for generating a dll -> replaced with -shared
-D is for setting a constant in the source -> same on both
-MT is for multithreading -> i was not able to find the mingw option for this as it seems it is not fully supported.
Am i wrong?
But it won't compile and i don't really know why.
Errors i got from testing are ether
-Cannot export samplefunction#4: symbol not defined
or when changing the command through guessing
-Error: invalid Suffix
-Warning: ‘somevariable’ initialized and declared ‘extern’
If you can help me, please do so. Thanks.
To compile a shared library from functions in a C++ code file:
g++ -o esample.so -shared -fpic -DNO_TRACE=1 sample.cpp
Multithreaded (-MT) should be the default these days.
The -LD equivalent is -fpic and -shared
Reference: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

Append compiler flags when running make

I would like to append flags to the compiler flags when running make, without altering the Makefile in anyway, e.g.
make CXX_FLAGS+='-DDEBUG'
The above treats "+=" as "=", so it's not the correct symbol.
You just have to modify the variable as override in your Makefile once. And then you can do what you want to do.
Here's the example,
Makefile:
override CFLAGS+=-g
app: main.c
gcc $(CFLAGS) -o app main.c
Run the make:
$ make
gcc -g -o app main.c
Append the '-Wall' to $CFLAGS from the command:
$ make CFLAGS=-Wall
gcc -Wall -g -o app main.c
Work fine here. And here's manual you can reference.

How to link a library with clang and llvm-link

I am compiling my program like this,
clang++ -O4 -emit-llvm file1.cpp -c -o file1.bc -pthread
clang++ -O4 -emit-llvm file2.cpp -c -o file2.bc -pthread
llvm-link file1.bc file2.bc -o main.ll -S
How do I specify linking with -ldl
llvm-link is a program which "links" together LLVM IR files into a single IR file; you can read more about it here. It does not have any relation to ld or to linking object files together.
If you do want to generate object code and/or executables, see these related questions:
How to generate machine code with llvm
llvm-link with external libraries
In short, you should be using native tools for assembling and linking (as and ld, for instance), though there is currently some experimental support for generating object files and for linking in LLVM.
In any case, Clang itself can invoke a platform linker - that is actually the default, but of course you have overridden by providing -c.