Compile string of C code - c

Really off the wall question here, but is there a way to compile a string of C code in GCC without any medium to hold that string (eg. a source file)?
Something along the lines of:
$ gcc "#include <stdio.h> int main( void ){ printf('hello world'); return 0;}" -o test
Feels really dirty, but it would be really nice if there was some simple way to do that type of thing.

If you use - as the input file in the command line, gcc reads from standard input. Since there is no file name extension gcc could use to find out the language that should be compiled, it has to be specified with the -x flag:
$ gcc -x c -o tst - <<EOF
> #include <stdio.h>
> int main(void) {
> printf("Hello world\n");
> }
> EOF
$ ./tst
Hello world

I confess to sometimes highlighting code and using:
(cat preamble.hpp; xsel) | g++ -x c++ - && ./a.out
The same works with "-x c" for C.

Related

Build a program with mingw on linux and run under wine --- UTF-8 support

I have C code that prints some characters. I would like to see these characters printed, as opposed to an alphabet soup,
#include <stdio.h>
int main() {
const char* test = "Greek: αβγδ; German: Übergrößenträger";
printf("%s\n", test);
}
What works:
Native Linux compilation: gcc test.c -o test && ./test
Winegcc compilation: winegcc test.c o test.exe && ./test.exe
MinGW64 compilation when the output is not a terminal : x86_64-w64-mingw32-gcc -o test.exe test.cpp && wine ./test.exe | cat
All three commands print Greek: αβγδ; German: Übergrößenträger to the terminal.
What doesn't work:
MinGW64 compilation when the output is a terminal: x86_64-w64-mingw32-gcc -o test.exe test.cpp && wine ./test.exe
This prints Greek: αβγδ; German: ÃbergröÃenträger to the terminal.
It looks like the MinGW runtime detects if the output is a terminal/console, and does exactly the wrong thing.
What I have tried:
system("chcp 65001"); — same output (the chcp command succeeds).
x86_64-w64-mingw32-gcc -fexec-charset=utf8 -finput-charset=utf8 — same output.
SetConsoleOutputCP(CP_UTF8); — same output.
wineconsole cmd and run .\test from there — same output.
LANG=en_US.UTF-8 wine ./test.exe — same output.
_setmode(_fileno(stdout), _O_U8TEXT); — no output at all.
Most combinations of the above — no dice.
What I have not tried yet:
Compiling with a Microsoft compiler under Wine.
Compiling with MinGW under Wine.
Compiling and/or running the program under real Windows with either compiler.
How can I fix the non-working case?

how to run a code that accepts parameters on eclipse and bash?

i have this simple code that accepts numbers from the standard input and print them , i wrote this code on code blocks and it works .. now i want to run the same code on eclipse and i don't know how it's supposed to work ? also after that i run it on eclipse i need to run it on bash where i have a directory that includes tests and i nee to check my code with these tests but i can't figure how to compile this c program there !
this is this is the simple code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
int i;
int k;
int a;
printf("Enter size of input:\n");
scanf("%d",&x);
if (x<0){
printf("Invalid size\n");
return 0;
}
int *numbers=malloc(sizeof(int)*x);
printf("Enter numbers:\n");
for(i=0;i<x;++i){
scanf("%d",&numbers[i]);
}
for(k=0;k<x;++k)
{
a=numbers[k];
printf("The number %d is a power of 2 \n",a);
}
return 0;
}
also i am tried to compile this code on bash with this line :
-std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c compiled.o
what am i doing wrong ?
Use the following command. Works like a charm in ubuntu bash. You can just input the values in the terminal, after running the program.
gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main
Above a command generates a binary with the name main, Run the main file using following command.
./main
Then enter the your values.
To compile gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main
to run from bash and accept arguments from a file named 'testcasefile' ;type following
main < (path to file)/testcasefile. 3 as for howv to run from eclipse refer to
https://stackoverflow.com/a/16921891/6721448
Let's start anew. Make a new directory for your project main inside project directory a directory for test case testcase
mkdir -p main main/testcase
now make test cases
Test case 1
2
3
4
compile main.c as follows
gcc - std=c99 -Wall -o main main.c
execute out put main with test case
./ main < testcase/testcase1

Can't preload a function in C program

I wrote some code in nasm and I'm trying to implement it in a C program as a replacement of strlen through a shared library, but it doesn't work.
nasm code:
section .text
global strlen:function
strlen:
mov rax, 42
ret
C code:
#include <stdio.h>
size_t strlen(const char *s);
int main()
{
printf("%zu\n", strlen("foobar"));
return (0);
}
I compile the C program just using gcc without any arguments, and I create the shared library with the following commands:
nasm -f elf64 strlen.asm
gcc -shared -fPIC -o libasm.so strlen.o
Finally, I include the shared library:
export LD_PRELOAD=`pwd`/libasm.so
But it displays '6' where I expect it to display '42'.
I don't think the problem comes from my library, because I get segmentation fault when I execute the ls command with LD_PRELOAD.
I'm working on Ubuntu 16.04.
This is not related to nasm at all. A C equivalent of your strlen() function does not work either.
$ cat strlen.c
#include <stddef.h>
size_t strlen(const char *s)
{
return 43;
}
$ cat s.c
#include <stdio.h>
size_t strlen(const char *s);
int main()
{
printf("%zu\n", strlen("foobar"));
return 0;
}
$ make s
cc s.c -o s
$ gcc -shared -fPIC -o strlen.so strlen.c
$ LD_PRELOAD=$PWD/strlen.so ./s
6
What is happening here is that gcc is using its own built-in version of strlen() that cannot be overridden. If the C program that calls strlen() is recompiled to not use this built-in version of strlen(), then your shared library can override it.
$ rm s
$ make s CFLAGS=-fno-builtin-strlen
cc -fno-builtin-strlen s.c -o s
$ LD_PRELOAD=$PWD/strlen.so ./s
43
$ LD_PRELOAD=$PWD/libasm.so ./s
42

Compile without input

I'm trying to use GCC in a shell script, but i want to use no input file, but a variable that contain my code.
For now I do :
echo '
#include <stdio.h>
int main(){
int a=1,b;
printf("Donnez un nombre : ");
scanf("%d", &b);
a+=b;
printf("test%d\n",a );
return 0;
}
' > .temp.c
#Entrez le texte ici
gcc .temp.c -o .ex -std=c99
Can I do something like gcc $inputCode -o executable ?
Moreover, if someone know if I can execute this c code without generating any file ?
Thanks !
You can do: echo whatever | gcc -x c -o .ex - but I hardly think it's worth it.
An alternate that may be cleaner [you don't have do either \" or \']. Note that the trap example may not be completely correct, but is probably something you're trying to achieve.
cat << EOF | gcc -x c -o .ex -
int
main(void)
{
return 'a';
}
EOF
trap "rm -f .ex" SIGINT
.ex
rm -f .ex
But, you're still going to have to create an output ELF executable file. Partly because the OS needs the embedded ELF loader (e.g. /lib64/ld-linux-x86-64.so.2) to know how to load the executable.
I've done the "script creates C executable from source" many times and I just create the .c and the executable and just do rm afterwards. I do this in perl where the C text appears after __DATA__ and I do an unlink function call instead of rm

How to write program during compiling?

Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is 1.c
Then, while compiling
$ cc -o 1 1.c
int main()
{
printf("Hello World\n");
}
^D
$ ./1
Hello World
$
This is an old parlaour trick I guess
My program, tty.c:
#include "/dev/tty"
Shell:
$ gcc tty.c
int main() {
printf("Hey\n");
} *Ctrl-D here*
In file included from tty.c:1:
/dev/tty: In function ‘main’:
/dev/tty:2: warning: incompatible implicit declaration of built-in function ‘printf’
$./a.out
Hey
The most reasonable way to make compilation read a file would be #include, but it's not obvious to me how to make it read standard input in a portable way on all Unix systems (easy in Linux, thanks to the magic of /proc!, but that wouldn't be portable).

Resources