how to avoid writing main() too many times in C? - c

Let say I have 5 small piece of codes in C.
Every time I want to test each piece of code, I have to repeat this process:
#include <stdio.h>
int main()
{
// code piece go into here
return 0;
}
Is there way that I don't have to do this 5 times? I'm using Code::Blocks, that means I have to create 5 different projects, which I believe not necessary because each piece of code is small.

Is this really so hard? Every program you run needs a main function, and the text you've pasted there isn't very long. Also, people expect to see a main function in C/C++ programs. If you template this out somehow, you're just going to make your code confusing.
If the issue is that you have to make a project for every test you want to build, then I would guess you are not using your IDE correctly. Is there not a multi-target project type that lets you have multiple test programs without all the extra project files? If there is not, then perhaps you should be using a different IDE.

Use a good editor with code templates. Most feature-full editors (Emacs, vi, Scite, Textmate, or even MSVC if that's your cup of tea) have some support for them. This way, writing this boring template every time will take only a fraction of the second.

Would template files or copying and pasting be too difficult for some reason?

Related

Adding new System Call in Minix

I am trying to create a new system call in Minix 3.3. At first i just want to create simple printmsg() call that will write "Hello World" on screen.
I looked various tutorials on internet and still couldn't find out solution.
I defined my sys call number in callnr.h like this #define PM_PRINTMSG (PM BASE + 48) and i increased number of sys calls #define NR_PM_CALLS 49.
In table.c I added CALL(PM_PRINTMSG) = doprintmsg.
In proto.h I described function prototype `int do_printmsg(void);
Function implementation is written in misc.c. I added #include <stdio.h> and made Hello World function int do printmsg(){ printf("I am a system call"); return 0; }
When I test my system call in user program _syscall(PM_PROC_NR, PM_PRINTMSG, &m); I don't get any errors but no printf is displayed.
So, is it possible to printf messages from system calls since i had to add <stdio.h> myself in misc.c or i missed some steps. I forgot to mention that i go in /usr/src/releasetools and type make services and make install respectively to recompile kernel.
I figured out what was the problem, so i will post answer if someone needs this in future. I did everything well in this example but i failed to compile kernel.
The location was correct which is usr/src/releasetools, but command needed is make hdboot. Also i figured out my PC somehow wasnt working well with this virtual machines and i had many errors while compiling even though i didn't change anything. When i switched to laptop everything worked fine.
My conclusion is sometimes there is just something wrong on your machine so you should try and test problems on different ones
In my opinion, with the continuous evolution of MINIX 3 and its series, it will be wise to only follow the developer's guide directly from the minix3.org website here
Although you managed to solve the problem yourself, the latest version of MINIX3 (MINIX 3.4) will follow a more advanced and suitable approach.
Please visit the link to learn more.
Many regards.
Ola

Mex C profiler Mac

I'm looking for a way to do very simple profiling in a mex program triggered from matlab. I compile from matlab using:
mex -O CFLAGS="\$CFLAGS -std=c99" rrt.c and then run my program.
Really all I need is a thing to see, which of two functions runs faster. However since it all goes down in about 1/100s time(NULL) is not fast enough.
Is there a simple function in C I could call, or are there any real profiling methods for a mex program in matlab?
I saw this post beeing treated as duplicate, but what I want to know is a way to profile the C code compiled with gcc in matlab, or easier some timing functions.
I use OSX 10.7.5 and matlab 2014b. Thanks for any hints.
Edit: Actually chappjc's hint got me looking for clock(), which does, what I need for the time beeing. An actual profiling would still be nice though.
The reason not to use tic/toc or similar is, that I have a base and a modified code, which both run with random samples. Compiling 2 versions of basically the same code each time I change something and having the extra step of exporting/importing the seed for the random number generator seems like a big hustle for exactly no value to me. I write code such that I don't have to repeat myselft. Having two seperate functions would need quite some duplicate code, since the changes are easy and a few, but deeply integrated in not just one spot.

Something wrong with C wrapper for Matlab

I'm currently facing an apparently silly problem with Matlab but I just can't figure out what is wrong (or better yet, I think it is wrong, but I'm being told it should work).
I have a Matlab script, "myscript.m" which needs to use a C-programmed function, "myfunc.c" which in turn has been compiled (or "lives") inside a library, "mylib.dll". In order for "myscript.m" to access and execute "myfunc.c" there is also a "myfunc.m" Matlab script, which is being called in "myscript.m". However, "myfunc.m" is a completely empty file, except for some comments (in no particular format or pattern either). All of these files were given to me and I'm being told that as it is, Matlab should correctly execute "myfunc.c" because "myfunc.m" is acting as a wrapper.
The problem I'm having is that it is of course not working. When I execute the line in "myscript.m" that reads:
output1 = myfunc(input1, input2);
I get Matlab errors saying that I'm trying to execute a script as a function. In my mind, this is correct, because I'm sending input and requesting outputs to something that is stated to have neither, since "myfunc.m" is empty (except for the comments, which are NOT code).
I think that Matlab has no way (as it is) to know that I want to access the C code inside "mylib.dll". I also DO NOT have a header file "mylib.h" so that I can load the library in Matlab using loadlibrary.
My question is, given the current description, could this execution work at all? Is there any way to make it work, or something that I'm missing? One possibility is that this is an old wrapper format and it currently doesn't work anymore.
If anyone knows anything or has suggestions, they will be greatly appreciated. Thanks!!!
Hugo
it sounds like myfunc.c is a mex file. try compiling it in Matlab using mex command. The dll extension is maybe old version matlab.
Have you tried with loadlibrary? You need a .h file but it's easy recreate on if you know the prototype of the function.

splint whole program with a complex build process

I want to run splints whole program analysis on my system. However the system is quite large and different parts are compiled with different compiler defines and include paths. I can see how to convey this information to splint for a single file but I can't figure out how to do it for whole program. Does anyone know a way of doing this?
Assuming you have a Makefile you could create a new target; then you would go through the actual compilation steps to duplicate them using Splint instead of the compiler.
My advice, however, is against the full-program approach. If you can isolate your system into separate parts, I'd rather start by checking them, one by one. Since your program is "quite large", expect a gazillion warnings... for each one of your modules. You will start to get rid of them once you have sprinkled your source code with the appropriate semantic annotations. Good luck! :)

Is it possible to write code to write code?

I've heard that there are some things one cannot do as a computer programmer, but I don't know what they are. One thing that occurred to me recently was: wouldn't it be nice to have a class that could make a copy of the source of the program it runs, modify that program and add a method to the class that it is, and then run the copy of the program and terminate itself. Is it possible for code to write code?
If you want to learn about the limits of computability, read about the halting problem
In computability theory, the halting
problem is a decision problem which
can be stated as follows: given a
description of a program and a finite
input, decide whether the program
finishes running or will run forever,
given that input.
Alan Turing proved in 1936 that a
general algorithm to solve the halting problem for all
possible program-input pairs cannot exist
Start by looking at quines, then at Macro-Assemblers and then lex & yacc, and flex & bison. Then consider self-modifying code.
Here's a quine (formatted, use the output as the new input):
#include<stdio.h>
main()
{
char *a = "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}";
int b = '"';
printf(a,b,a,b,b);
}
Now if you're just looking for things programmers can't do look for the opposite of np-complete.
Sure it is. That's how a lot of viruses work!
Get your head around this: computability theory.
Yes, that's what most Lisp macros do (for just one example).
Yes it certainly is, though maybe not in the context you are referring to check out this post on t4.
If you look at Functional Programming that has many opportunities to write code that generates further code, the way that a language like Lisp doesn't differentiate between code and data is a significant part of it's power.
Rails generates the various default model and controller classes from the database schema when it's creating a new application. It's quite standard to do this kind of thing with dynamic languages- I have a few bits of PHP around that generate php files, just because it was the simplest solution to the problem I was dealing with at the time.
So it is possible. As for the question you are asking, though- that is perhaps a little vague- what environment and language are you using? What do you expect the code to do and why does it need to be added to? A concrete example may bring more directly relevant responses.
Yes it is possible to create code generators.
Most of the time they take user input and produce valid code. But there are other possibilities.
Self modifying programes are also possible. But they were more common in the dos era.
Of course you can! In fact, if you use a dynamic language, the class can change itself (or another class) while the program is still running. It can even create new classes that didn't exist before. This is called metaprogramming, and it lets your code become very flexible.
You are confusing/conflating two meanings of the word "write". One meaning is the physical writing of bytes to a medium, and the other is designing software. Of course you can have the program do the former, if it was designed to do so.
The only way for a program to do something that the programmer did not explicitly intend it to do, is to behave like a living creature: mutate (incorporate in itself bits of environment), and replicate different mutants at different rates (to avoid complete extinction, if a mutation is terminal).
Sure it is. I wrote an effect for Paint.NET* that gives you an editor and allows you to write a graphical effect "on the fly". When you pause typing it compiles it to a dll, loads it and executes it. Now, in the editor, you only need to write the actual render function, everything else necessary to create a dll is written by the editor and sent to the C# compiler.
You can download it free here: http://www.boltbait.com/pdn/codelab/
In fact, there is even an option to see all the code that was written for you before it is sent to the compiler. The help file (linked above) talks all about it.
The source code is available to download from that page as well.
*Paint.NET is a free image editor that you can download here: http://getpaint.net
In relation to artificial intelligence, take a look at Evolutionary algorithms.
make a copy of the source of the program it runs, modify that program and add a method to the class that it is, and then run the copy of the program and terminate itself
You can also generate code, build it into a library instead of an executable, and then dynamically load the library without even exiting the program that is currently running.
Dynamic languages usually don't work quite as you suggest, in that they don't have a completely separate compilation step. It isn't necessary for a program to modify its own source code, recompile, and start from scratch. Typically the new functionality is compiled and linked in on the fly.
Common Lisp is a very good language to practice this in, but there are others where you can created code and run it then and there. Typically, this will be through a function called "eval" or something similar. Perl has an "eval" function, and it's generally common for scripting languages to have the ability.
There are a lot of programs that write other programs, such as yacc or bison, but they don't have the same dynamic quality you seem to be looking for.
Take a look at Langtom's loop. This is the simplest example of self-reproducing "program".
There is a whole class of such things called "Code Generators". (Although, a compiler also fits the description as you set it). And those describe the two areas of these beasts.
Most code generates, take some form of user input (most take a Database schema) and product source code which is then compiled.
More advanced ones can output executable code. With .NET, there's a whole namespace (System.CodeDom) dedicated to the create of executable code. The these objects, you can take C# (or another language) code, compile it, and link it into your currently running program.
I do this in PHP.
To persist settings for a class, I keep a local variable called $data. $data is just a dictionary/hashtable/assoc-array (depending on where you come from).
When you load the class, it includes a php file which basically defines data. When I save the class, it writes the PHP out for each value of data. It's a slow write process (and there are currently some concurrency issues) but it's faster than light to read. So much faster (and lighter) than using a database.
Something like this wouldn't work for all languages. It works for me in PHP because PHP is very much on-the-fly.
It has always been possible to write code generators. With XML technology, the use of code generators can be an essential tool. Suppose you work for a company that has to deal with XML files from other companies. It is relatively straightforward to write a program that uses the XML parser to parse the new XML file and write another program that has all the callback functions set up to read XML files of that format. You would still have to edit the new program to make it specific to your needs, but the development time when a new XML file (new structure, new names) is cut down a lot by using this type of code generator. In my opinion, this is part of the strength of XML technology.
Lisp lisp lisp lisp :p
Joking, if you want code that generates code to run and you got time to loose learning it and breaking your mind with recursive stuff generating more code, try to learn lisp :)
(eval '(or true false))
wouldn't it be nice to have a class that could make a copy of the source of the program it runs, modify that program and add a method to the class that it is, and then run the copy of the program and terminate itself
There are almost no cases where that would solve a problem that cannot be solved "better" using non-self-modifying code..
That said, there are some very common (useful) cases of code writing other code.. The most obvious being any server-side web-application, which generates HTML/Javascript (well, HTML is markup, but it's identical in theory). Also any script that alters a terminals environment usually outputs a shell script that is eval'd by the parent shell. wxGlade generates code to that creates bare-bone wx-based GUIs.
See our DMS Software Reengineering Toolkit. This is general purpose machinery to read and modify programs, or generate programs by assembling fragments.
This is one of the fundamental questions of Artificial Intelligence. Personally I hope it is not possible - otherwise soon I'll be out of a job!!! :)
It is called meta-programming and is both a nice way of writing useful programs, and an interesting research topic. Jacques Pitrat's Artificial Beings: the conscience of a conscious machine book should interest you a lot. It is mostly related to meta-knowledge based computer programs.
Another related term is multi-staged programming (because there are several stages of programs, each generating the next one).

Resources