Get preprocessor output from C through MATLAB mex - c

I want to get the preprocessor output when compiling my c-code through mex of MATLAB using MinGW64 Compiler (C) so using gcc (right?).
From this post I got that you can do this with pure gcc passing the option -E to gcc.
However I installed gcc through MATLAB app and therefore cannot access it through command line (would also appreciate a command how to do that, without reinstalling MinGW64 and setting it up manually for use with MATLAB).
I tried to do the following assuming that compiler flags are the right way to pass the argument:
mex -c grampc_run.c -I../../include -I../include COMPFLAGS='$COMPFLAGS -E'
This just results in the creation of the object file.

COMPFLAGS is used by the MSCV compiler. The GCC compiler loos at CFLAGS and CXXFLAGS (for C and C++ compilation, respectively). See here. Thus, you should use the following syntax:
mex -c grampc_run.c -I../../include -I../include CFLAGS='$CFLAGS -E'
You might also want to add the -v option to mex. GCC puts the preprocessor output to the standard output, which mex might not show you. With -v it does show you all the output.

Related

How to compile a simple C code with "$gcc test.c -o demo" command?

On this beginner tutorial at TutorialsPoint, it says to save the code as test.c and compile it using "$gcc test.c -o demo" command at CMD.
But I don't have $gcc. What is it?
Let's split this into parts:
$ is a character indicating that the shell is ready to receive a command. It is not part of the command.
gcc is an executable executing the GNU C compiler from the GCC toolchain.
test.c -o demo are arguments supplied to gcc.
The GCC toolchain is only available natively for GNU systems such as Linux. Using MinGW or CygWin you can ape its functionality, though.
Notes:
A nice comment, which I second, to your question by #iharob:
Don't use gcc test.c -o demo specially if you are a beginner, use gcc -Wall -Wextra -Werror test.c -o demo.
The additional switches make the compiler point out more warnings.
GCC (GNU Compiler Collection. Upper case.) is a set of compilers, that can compile several languages.
gcc (lower case) is a command that compile the code you wrote, using the C compiler that GCC includes, into a working C program. Similar commands are g++ for C++ code, gcj for Java code, etc.
Note GCC is intended for Linux or other Unix-like systems (You can use it in Mac OS X with the help of xcode). If you are using Windows, consider [MinGW] (http://www.mingw.org/) or CygWin https://www.cygwin.com/.
As a beginner, if you still have trouble, consider use Dev-C++, an IDE (integrated development environment) that compiles C and C++. It does all the compiler things for you.

Parse single file from a GNU Unix utility program with clang and llvm

I want to do some program analysis project on C program. So far, I want to get one C program as the input for clang, and then I use LLVM to make some analysis.
I want to do some experiment on gnu unix utility. For example:
diff (https://www.gnu.org/software/diffutils/).
For example, when I want to do analysis on src/diff.c:
My command is: clang -emit-llvm -O0 -c diff.c -o test.bc
However I will get the below error:
./system.h:21:10: fatal error: 'config.h' file not found
which seems that clang cannot find config.h.
So my question is how to parse single program in a GNU utility?
Do I need to write a Makefile or where can I find something like config.h?

Disable compilation and linking in Code::Blocks?

Programming newbie, I want to disable the compiler/linker and just look at the precompile/ preprocessor's preprocessed code for a program...not sure what this would be called or what the usual method is for doing something like this.
Using the GNU GCC compiler in Code::Blocks, and I looked thru all the various options but not sure the command or what the menu item is called/labeled.
gcc -E source.c -o myfile.i
Here -E is a flag stand's for PRE-Process only.
And -o is another flag which stores the PRE-Processed output of source.c into myfile.i (here .i is common extension given for PRE-Processed files in gcc)
You can use the following option to see the pre-processing files. Normally the compiler will create the files on the fly while trying to create an object file. But at the end removes them.
So in order to view them you can use the command with save-temps.
The output will have the following files:
hello.i-Pre-Processed Output
hello.s-Assembler Output
hello.o-Compiler Output
gcc -save-temps hello.c

How compile a llvm code file using functions from C?

Is there a way for compile a file in llvm (*.ll) that uses functions in C?
I created a test using check C and I compile it as:
$clang counter_i.c counter_test_check.c -lcheck
This way, I am using the libraries from check, but I need produce the llvm code that uses the library from check. When I try this command:
$clang -S -emit-llvm counter_i.c counter_test_check.c
and try execute the code:
$lli-mp-3.5 counter_test_check.ll
I receive this answer:
LLVM ERROR: Program used external function 'srunner_create' which could not be resolved!
I think that a solution is do something as:
$clang -S -emit-llvm counter_i.c counter_test_check.c -lcheck
But it is not supported.
I am thinking that a similar answer is available at:LLVM JIT-compiled program cannot find external functions
Yes, LLVM has a C interface (although there may be some limitations compared to the C++ API):
http://llvm.org/docs/doxygen/html/group__LLVMC.html
I found a solution with:
clang -S -emit-llvm -c counter_test_check.c counter_i.c
clang -o executable counter_test_check.ll counter_i.ll -lcheck
./executable
It does the compilation in two steps and this way I can use other llvm source file.

How do I set gcc to use the file extension (.c or .cpp) to determine the correct compiler/linker?

I have a simple, representative C program, stored in a file called hello.c:
#include <stdio.h>
int main(void)
{
printf('Hello, world\n');
return 0;
}
On my Linux machine, I attempted to compile the program with gcc:
gcc hello.c
which returns an error:
undefined reference to "___gxx_personality_v0" ... etc
As has been discussed before in the context of C++, this problem arises in the linking stage, when gcc attempts to link C libraries to a C++ program, thus giving the error. In one of the answers, someone mentioned that the extension does matter, and that gcc requires the .c extension when compiling C files, and some other extension (e.g. .cpp) when compiling C++ files.
Question: How do I set gcc to use the file extension to determine which compiler to use, since gcc seems to be defaulting to C++ on my system? Specifying the language through the file extension alone doesn't seem to be enough. If I specify the language using the -x flag, gcc functions as expected.
gcc -x c hello.c
Typically, you let make decide this.
GNU Make has built in implicit rules, which automatically pick the right compiler.
Try a Makefile with these contents:
all: some_file.o some_other_file.o
And then place a some_file.cpp and some_other_file.c in the same directory, and gnu make will automatically pick the correct compiler. The linker, you may still have to provide yourself. When mixing C and C++, it's usually easiest to link with g++, like so:
program.exe: some_file.o some_other_file.o
g++ -o $# #^
This is the same as:
program.exe: some_file.o some_other_file.o
g++ -o program.exe some_file.o some_other_file.o

Resources