Create a program from another program? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm making an application that will create a working executable based on what the end-user inputs into the program.
For example:
if (make_annoying_sounds == true)
{
//Generates an executable that makes annoying beeping sounds
}
else
{
//Generates an executable that doesn't make annoying beeping sounds
}
Basically I want my program to generate/create another program. I've seen/used many programs that do this. I have searched all over the internet and can't find anything. All help is appreciated. (Create a program, from within my program).

Try using a basic system call to invoke a compiler after you've created the source file.
You can create the source file with just the utilities found in stdio.h
Security Note: The system function is known to be dangerous. When in doubt, call a function like exec to invoke the compiler. Although exec erases the currently running process, so you should use fork and then call exec if you want to keep doing stuff after the compilation has finished.

So you want to create a compiler? This question below contains a whole list of resources to help you get started.
Learning to write a compiler

You need to do the following:
Based on the user input, generate the code for the custom program.
Compile that code into an executable file.

Theorically, you could, depending on what the user inputs, make your C code generate C-code inside your if statements. However this would be quite difficult.
The best way I think is to make an independent C engine which will only implement functions that any of the generated program can execute (playing the sound given in parameter, for example). The program you are trying to code (not the engine, but the one with the if statements, let's call it the "master program") must generate code which implements the algorithm which will choose what function of the engine to call and when. This generated code should be written in a scripting language like lua, since in is easier to generate script code than C. Thus, the engine should be designed to be able to communicate with Lua scripts. When the user clicks on the final "generate program" button of the master program, the master program calls gcc to compile the engine and the Lua script to generate the program the user tried to create. This is long, but this is, I think, the right way to do it.

Related

python "print" like function in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to C and I use to code in python, and I usually print too much variables and text to test my code and printf statement is irritating sometimes. I want to write a function in c which works exactly like print function in python.
I am facing two problem while writing the function.
I want to take n number of arguments as input to the function.
I want to take any data type as input to the function,
for Eg: print(12); print("hello"); print(123.12) should not raise error.
What have I done so Far
I found solution for taking n number of arguments using <stdarg.h>, but I have to specify the data type of the first argument which is not what I want.
"generic selection" Macros can be used to call different function on different data type of argument passed but not sure how it can be done, here is the link which I used for reference.
I want to write a function in c which works exactly like print function in python.
You simply cannot do that (because of type erasure : at runtime, type information is lost in C). Read Modern C then see this C reference.
In practice, you'll better write one function per datatype in C to print it. So it would be void print_int(int); to print an integer, void print_double(double); to print a double, etc. Once you have a collection of such functions you might use _Generic inside a macro (but that is rarely useful; it can handle a finite set of types).
Study for inspiration the source code of existing open source C software on github or elsewhere. Look for inspiration inside the source code of GNU bash, sqlite, GTK or GNU bison (and perhaps inside the source code of Python; half of the Python interpreter is coded in C)
Read also the documentation of your C compiler, e.g. GCC. So compile with all warnings and debug info: gcc -Wall -Wextra -g then use the GDB debugger.
Perhaps you want to implement some tagged union abstract data type in C. Then take inspiration from GNU guile or the runtime of Ocaml and dive inside their source code.
Once you are more familiar with C, read some C draft standard, e.g. n2176
Consider using Frama-C or the Clang static analyzer.
Consider also sometimes generating some C code, like SWIG does.
Budget weeks of work.

How to create my own libraries to get started with an OS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am learning OS concepts. I created a simple boot loader in ASM and a looping kernel file in C. I compiled them both and it works.
Now I need to print some text using kernel, which is in C. I learnt that while on developing an OS, usage of standard library functions must be omitted. So how can I create my own library. I mean to print a text to screen without including any standard header file. How can I do that? Should I use inline assembly or any other methods?
Making it so simple, my question is, How C language can interact with the hardware without standard libraries?
I do the same 15 years ago :) , you are forced to use your own kernel functions, based in the HAL concept (hardware abstraction layer). You build a HAL based module, as an example, a screen output driver. This driver should be built with two sides: The side who is in contact with your own custom OS, and the side who is in contact with your very specific hardware. So, if at any given moment you change the hardware then your OS is not affected by this change. This is called (formal) : Interface. Is a software pattern concept.
Good look. is a very intersting project :)
As a pseudo code example:
// yourclientprogram.c
include "screendriver.h"
i = new instance of LCDScreenDriver;
i.selectScreen(0);
i.printf("%s","hello");
So, your kernel files will looks like this:
//screendriver.h
class LCDScreenDriver extends ScreenDriver {
protected function output(data){
...very specific ASM code for your LCD monitor goes here..
...this code is very specific to output a -data- buffer
...and nothing more than this
}
}
class ScreenDriver {
protected function output(data); // a virtual pure function
public function printf(args,...){
dataTobePrinted = ..make your own printf methods...
this.output(dataToBePrinted);
}
}

display filename, line number and function in C without modifying the source code [duplicate]

This question already has answers here:
how to trace function call in C?
(10 answers)
Closed 8 years ago.
I am new in a company, working with C source code which almost lacks any kind of tracing mechanism.
I would like to know whether or not the application passes through a certain file and where (which function).
I could do this using breakpoints, but the concerned file contains a huge lot of functions.
Therefore I'm looking for some kind of tool, that I can attach to the application, and that gives an output of following kind:
-- Main.c (main_function())
---- submain.c (submain_function())
...
From that, I then could deduce where (which filename, which function) the application is passing.
Does anybody know whether or not such a tool exists?
Thanks
If you're on linux, gdb might come handy.
You can compile the code using -g or -g3 option with gcc, then run the binary using gdb ./<executable_name>, set a breakpoint on desired function in any of the source files and check the call.
While stepping through the application, it will show the filename and line number of the executing instruction.
Note: Please check this and this for a detailed understanding.
I assume you develop on Linux. Then you could also customize the GCC compiler, in particular using MELT (a lispy domain specific language to extend GCC), to have the compiler add some logging at many places. For that you'll need to insert a new GCC "optimization" pass doing such a job, and most importantly, you'll need to understand some details about GCC internal representations (Gimple, Tree-s, Basic blocks, ...)
However, that would probably require more than a week of work from your part. Unless your code base is really big (at least half a million
of lines) that might not worth the effort

Hiding Lua Source Code In C Application

I am currently developing a game in C and Lua. Because I plan to sell my game when it is finished, I would like to keep the source code closed. So my question is whether there is a way I can hide, or somehow access my Lua code from C, without the user being able to look. Right now, my executable is placed in the same place as my Lua code so it can be accessed.
Thanks for reading this, and any help is appreciated. Please ask me for more details if I am being too vague.
I think the correct answer is, that you can't. You can only make life harder for the cracker. Better protection schemes than compiling code into bytecode have been cracked. If your game does not prove popular, it won't matter anyway. Write the game first, then worry about hiding your code.
Lua manual says:
[Lua] Chunks can also be precompiled into binary form; see program luac for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly.
This means you can use luac (Lua compiler) to compile your Lua code to binary form, which will not be easily readable, but can still be disassembled to find out what it does (which can be done even with C if you are determined enough).

How do I generate a .cpp file from source code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm starting with C programming, and have written a program and am ready to compile. I've heard that mingw is a good choice, but the documentation for using it starts with a .cpp file and then turns that into a .exe. What I currently have is just the pure source (i.e. just text commands), how do I turn that into a .cpp? Thanks.
A cpp file is a C++ source file. Many tools (compilers, editors, etc.) can work with .c files or .cpp files.
A source file refers to the actual code you write. You know, stuff like
int main() {
printf("Hello, world!");
return 0;
}
That would be source code. 4 lines of it. So generally, you can't generate it. You have to write it. It sort of sounds like you wrote a .c file and are getting confused because you're using a tool whose example uses a .cpp file.
If the tool works with both C and C++, then use it with the .c file.
If it only works with C++... then you'll have to use something else.
If you have written code in a file like "xyz.txt" then simply rename it to "xyz.c". Also, you can take a file named like "xyz.c" create a copy of it and rename that copy to "xyz.cpp" and then modify "xyz.cpp" so that it can be compiled as C++ code.
In either case the "xyz.c" when compiled becomes "xyz.exe" and "xyz.cpp" will also become an "xyz.exe" load module, but not the same as the one generated from C code.
If you want load modules for a program written in C and the same program written in C++ then you will have to give them different names, like: "xyz.c" and "abd.cpp".
Hope this helps, and welcome to our world of programming!

Resources