Compile preprocessor output using gcc - gcc

I've generated a preprocessor out using the -Ecommand.
Is it possible to compile exactly that generated file using gcc?

Yes, just save the output as a C or C++ file (or whatever the input was).
That's exactly what GCC does, schematically speaking: preprocess, then compile.

Related

How to hide a single header file from gcc

I am compiling a c++ project and trying to find all what all functions from ncurses.h are used throughout the project.
I was wondering if I can tell gcc to not include specifically ncurses.h?
I ended up passing -D__NCURSES_H=1 on command line. 🤷‍♂️

How can I suppress assembly files output by compilation proper?

I have some C++ library code that I want strictly compiled for a quick check, and I don't want any files produced to be used for later stages (assembly, linkage, etc.)
I can do
g++ -S main.cpp
but this will give me an assembly file that I'm just going to wind up deleting anyway.
Is there an option that will tell the compiler to just compile a source file but don't produce any files?
EDIT[0]: I'm using mingw on Windows.
gcc has the option -fsyntax-only:
Check the code for syntax errors, but don’t do anything beyond that.

Assembler used by golang when building with and without cgo

Let's say I have a golang package, which contains some assembly code:
demopkg/
source1.go
source2.go
asm_amd64.s
If I try to build it using go build, toolchain will use go tool asm to assemble the *.s files.
But if I add Cgo to the mixture, by putting a single import "C" into any of the sources, go will switch to gcc assembler.
I can see it by executing go build -n. Calls to the /usr/local/go/pkg/tool/linux_amd64/asm from the first case get replaced by calls to gcc. Besides that, it starts complaining about broken syntax.
Is this behaviour documented, so I can rely on it for the maintaining of my package? Can I force go build to use one exact assembler?
Yes, it's in the cgo documentation
When the Go tool sees that one or more Go files use the special import
"C", it will look for other non-Go files in the directory and compile
them as part of the Go package. Any .c, .s, or .S files will be
compiled with the C compiler. Any .cc, .cpp, or .cxx files will be
compiled with the C++ compiler. Any .h, .hh, .hpp, or .hxx files will
not be compiled separately, but, if these header files are changed,
the C and C++ files will be recompiled. The default C and C++
compilers may be changed by the CC and CXX environment variables,
respectively; those environment variables may include command line
options.

NVCC separate compilation with PTX output

Just to see what kind of code CUDA is generating I like to compile to ptx in addition to an object file. Since some of my loop unrolling can take quite a while I'd like to be able to compile *.cu→*.ptx→*.o instead of wasting time with both *.cu→*.ptx and *.cu→*.o, which I'm currently doing.
Simply adding -ptx to the nvcc *.cu line gives the desired ptx output.
Using ptxas -c to compile *.ptx to *.o works, but causes an error in my executable linking: Relocations in generic ELF (EM: 190).
Attempting to compile the *.ptx with nvcc fails silently, outputting nothing.
this image is quite helpful:
Is there some option I need to pass to ptxas? How should I properly compile via ptx with separate compilation? Alternatively, can I just tell nvcc to keep the ptx?
Alternatively, can I just tell nvcc to keep the ptx?
Yes, you can tell nvcc to keep all intermediate files, one of which will be the .ptx file.
nvcc -keep ...
Keeping all the intermediate files is a bit messy, but I'm sure you can come up with a script to tidy things up, and only save the files you want.

Linking a .s file with CMake

I have a c function that I'd like to use but thats compiled with the Intel compiler instead of the gnu C compiler. I'm using cmake to build the program.
(I'm actually using ROS and hence rosmake but the base is cmake so I think its more of a cmake issue than a ROS issue).
Suppose the file built with icc is x.c and produces an x.s file. I want to use the function a() from x.c in my file y.cpp.
In y.cpp I have:
#include "x.h"
.....
call a()
which works if CMakeLists.txt has
rosbuild_add_executable(y y.cpp x.c)
rosbuild_add_executable is analogous to add_executable(...)
but if I build x.c with icc and try to include the x.s file instead:
rosbuild_add_executable(y y.cpp x.s)
It doesnt work. Is there some change I should make to the way I call a() in y.cpp? or is there some other way to link it.
When using gcc, you can compile .S files with your C compiler (no explicit invocation of asm needed). CMake can be told to do so using
set_property(SOURCE <myfile>.S PROPERTY LANGUAGE C)
for each of your .S files.
To work with .s files you'll have to enable assembly language support in CMake with enable_language.
You can find more information here: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/languages/Assembler

Resources