TkTkorrovi 69 Junior Poster

I don't know, this maybe http://sourceware.org/autobook/autobook/autobook_toc.html But maybe someone knows something better.

TkTkorrovi 69 Junior Poster

You don't have to understand the makefile, to use the video codec. It makes no sense to read the automatically generated makefile, then better learn autotools. It is very simple to use autotools for a simple project, but for bigger projects it's not that simple any more. They use autotools because these supposed to be very powerful tools, indeed they help to configure, but they are not that powerful, like they cannot be used for compiling kernel modules.

TkTkorrovi 69 Junior Poster

I think this should work, but i didn't test it, please do it yourself.

#include <stdarg.h>

void stringstofile (FILE *filehandle, ...)
{
	va_list vargs;

	va_start (vargs, filehandle);
	while (va_arg (vargs, char *))
		fputs (va_arg (vargs, char *), filehandle);
	va_end (vargs);
}
TkTkorrovi 69 Junior Poster

I would suggest to tokenize the string first.

TkTkorrovi 69 Junior Poster

What you talk about is not called "parallel programming" but event-driven programming. Event-driven programming is common for graphical programs which use widgets and windows. I wrote a snippet using GTK http://www.daniweb.com/code/snippet737.html where you can see how the event-driven program works. You can use GTK drawing area if you like, but the general principles are the same. There, in the end of the function main, is a loop that is called a "game loop", which is for anything that has to happen all the time continuously. But you see that the special functions shall be registered for events, such as key press, delete and destroy. These functions are called the event handlers, so that when some event happens, the function which handles that event would be called, while the main loop would go ahead as if nothing happened.

TkTkorrovi 69 Junior Poster

C wiki, with a lot of resources http://www.iso-9899.info/wiki

~s.o.s~ commented: Excellent. +29
TkTkorrovi 69 Junior Poster

gcc can compile the code for ARM processors, powerpc, and some others, it depends what processor your microcontroller use. In general, the most feasible way to program microcontroller, is still to test and compile the code for your microprocessor, and then write it into the chip. For testing the program, you most likely need to emulate the hardware of your microcontroller, like as some debug code. It again depends on your microcontroller, there should be some debuggers available for any controller type, which would help you to test your program in the right environment.

TkTkorrovi 69 Junior Poster

Usually, your compiler program also does the linking, ie invokes the linker. So in fact, all you need, is to write all your source files in the command line of the compiler, like this:

gcc source1.c source2.c -o executable

But if you compiled the sources separately, then you use the same compiler to link these files:

gcc source1.o source2.o -o executable

This is how the compiler finally runs, no matter whether invoked manually, by make or by ide. Ide is bad because it's confusing and difficult or sometimes impossible to use for bigger projects. But mostly makefiles are used to put together this compiler command line. Before the makefiles were used because it saved time, so that only the changed files had to be compiled, this is true for bigger projects also today, compiling of which may take some ten minutes. But the computers are fast today, so for smaller projects the makefiles would not make sense for that reason, and it may even be ok to use command line directly. But makefiles do also some other things, so some programs, like kernel modules, cannot be compiled without a special makefile, but other programs may configure better with makefiles, or such.

TkTkorrovi 69 Junior Poster

It is dangerous if you include the same c file in more than one file. The linker doesn't complain about multiple declarations but it complains about multiple definitions. This is why the definitions and declarations are usually split into two files, the *.c file for definitions, and *.h file for declarations, then we can safely include the *.h file everywhere, both where we defined these things, and to every file where we use them.

TkTkorrovi 69 Junior Poster

You need to write for functions, prototypes (except where you define it if it is before the other functions which use it, but good if they are there even then), and for global variables, declarations, and exactly the same prototypes or declarations both in the file where you define function or give a value to a global variable, and in the file where you use them. If the linker finds that the declarations are the same in two or more modules, it considers that they refer to the same function or variable. That's all, as simple as that, just do it correctly.

TkTkorrovi 69 Junior Poster

Oh sorry, something with my attention, <> in include statement means that the files would be included from a system directory, there are some default include directories for every compiler. But you need to put the file name between the "", if it is in your project's directory. So #include "my.h" or #include "my.c". Sorry again.

TkTkorrovi 69 Junior Poster

You likely don't link your my.c anyhow. It depends on your compiler, like gcc main.c my.c -o main both compiles the modules, and links them, producing the executable main. This is similar for all compilers, but the problem is when you use some graphical ide, which is bad anyhow, but if you do, you must add your my.c in project, so that the ide knows what files need to be linked. Usually, the function prototypes and global variables defined in one file,would be declared not in that file, but in include file, like my.h, which would be included to both files, with #include <my.h>. It works also otherwise, but this is a good habbit, so that you don't always have to write your declarations separately in every file. If you are very lazy, your program would certainly compile if you would include your c file to the main file, with #include <my.c>, but it's not good to be that lazy.

TkTkorrovi 69 Junior Poster

What do you mean by graphics? If you want to write some games, you should use either opengl for 3d or sdl for 2d (both cross-platform). But if you want edit boxes, menus etc, and 2d graphics also, the best is GTK http://www.gtk.org. Because it's not commercial like qt, and it's for C, using it you'll also understand how to do the object-oriented programming in C (I mean C, not C++). GTK is available for Linux, Windows and Mac OS X. It's the best joice because it is cross-platform, it is not right to choose a library based on Windows API, because Windows API always changes, and one day we may have not Windows, but a new operating systems, so if you want that your programs would work and there would also be any use of your knowledge in the future, write programs which at least can be ported to Linux. GTK can be installed in Windows, and used no matter what compiler you have, but its installation is somewhat complicated, as you have to manually install many components into a directory tree. So in Windows, the best is to use cygwin, which installs everything automatically, just like Linux.

SpS commented: Good ~SpS +3
TkTkorrovi 69 Junior Poster

All escape sequences which c89 standard defines -- a single-quote ' can be represented by \', " by \", ? by \?, \ by \\, form feed by \f, new-line by \n, carriage return by \r, horizontal tab by \t, vertical tab by \v, null character by \0, octal integer by \ octal digits, and hexadecimal integer by \x hexadecimal digits; the escape sequences \a and \b are implementation-defined. The bash shell also accepts almost all of these, to be accepted in a string, the string must be in the form $'string', not 'string' or "string".

TkTkorrovi 69 Junior Poster

It seems that what concerns the programming, there are so many opinions that it's even not worth to discuss. termios.h is a POSIX header, and POSIX is standard, the only standard of an operating system, and even Microsoft has some POSIX kit, even Windows somewhat follows that standard. Windows are a programming concept, not only graphical windows what we see on the screen. But in Windows, it's really likely the only way to get any kit which has windows.h, and use non-canonical input and moving cursor from there. windows.h is about windows api, it's not any standard though. But of course it's necessary to ask what kit the instructor has.

TkTkorrovi 69 Junior Poster

It depends what you need to do. If you can do it with canonical input, you can just rewrite the screen every time something changes, the problem with this is that it's slow, and is not a good solution when something changes very often, especially on Windows console which is extremely slow. And yes, without cleaning screen it may show some flickering, it's not exactly a good quality, but then for some university tasks it may even be enough, and the program written like that can later be very easily ported. The canonical input is mostly the biggest problem, because many such interactive programs also need some moving around, like for selecting something, and doing that with canonical input is just much too inconvenient. BTW, system ("cls") clears the screen in Windows, but it can only be used when it will not occur very often, and i didn't really try whether the cls command in Windows also sends some control sequence to console, though i doubt that it doesn't.

TkTkorrovi 69 Junior Poster

It seems that you like to write some console/terminal applications. Please try to understand me now. Standard C doesn't provide any features for that. Yes it's somewhat stupid to be able to write only some command-line programs, for example, while learning C. But C standard library is anyway only a fraction of POSIX, so it makes no sense to be restricted with C standard library. And such console/terminal programs can be written using only standard C and POSIX. We need really two things -- non-canonical input and clear screen. The code below shows how we can set the non-canonical input using termios.h, this code clears screen and prints immediately any character which you enter. Clearing screen is somewhat more tricky, because POSIX doesn't provide any function or control sequence to clear the screen. It provides a command "tput clear" for that though. So the only way to clear screen quickly using only the POSIX features, is to get the control sequence which "tput clear" outputs, and then print it every time to clear screen. The code below gets that control sequence using popen.

And now again, how can we move the cursor. POSIX does not provide any control codes, functions or utilities to move cursor. And if to think about it more deeply, *you do not need to move cursor*, the gotoxy kind of functions for console are somewhat similar to goto functions in c, both seem to be convenient, but would cause a bad programming. It is that …

TkTkorrovi 69 Junior Poster

Well, I called gcc (http://gcc.gnu.org/) a linux
compiler because it is mostly used on linux, it is the
most often used compiler on linux. See about c standards
http://www.cse.ohio-state.edu/cgi-bin/info/info/gcc,Standards,
also compiles c++, java, fortran and ada. MingW is gcc compiler
ported to Windows, and libraries. The c and c++ libraries for
gcc are complete, but the posix (unix standard) libraries
are only partially implemented. On Windows, cygwin should
be used for full posix support, but this needs a cygwin dll
file for programs to run. BTW, dev-c++ mentioned here, uses
mingw as well, you can also use gcc on command line, when you
have dev-c++. There are also several other ide-s for mingw,
or which can be used with mingw, like visual-mingw, relo,
vide (based on vim), and red hat source navigator (available
on mingw download page).

TkTkorrovi 69 Junior Poster

As a reply to the previous post, mingw is public domain, other things are at least glpl, so you can not only use them for free, but also write a commercial programs with them, and sell these programs, without paying anything to anybody.

About installing mingw, in case you don't know, it's easy, just download it and run the installer. But then, say you use windows 2000 or xp, go to start > settings > control panel > system > advanced > environment variables. There is a box called user variables. See if there is a variable PATH, if there is, press edit, otherwise press new and create that variable. This is a set of paths separated by semicolon, the path you should add there is \mingw\bin, if you installed mingw under the directory \mingw. Press OK everywhere, and the settings change immediately, you don't have to reboot. Now when you run gcc on start > run > cmd, or :!gcc from within vim, the gcc says "no input files" then the mingw works, get some snippets here, compile and run them. That's it, not so very complicated. And you don't need any commercial compilers or ide-s.

TkTkorrovi 69 Junior Poster

Hi!

I usually use mingw http://www.mingw.org, as this uses a linux compiler gcc, and is therefore very reliable and complies to standards more than any other. It is easier to work with bigger projects using command line, and not so much rubbish files either. I use a vim editor http://www.vim.org, in insert mode it works almost like notepad, but it is useful for so many things. First, it enables to run programs from within editor, which is useful among other things because that way you can run programs in the directory where you are, as windows explorer doesn't change the directory of the shell, it usually remains c:\. And then, to compile, say you use makefile, then you may write a bat file like build.bat, with a command like this: redir -e make.txt mingw32-make -f makefile, of course you can tell vim to run that for compiler as well. Or you can write there the compile command directly, makefile is something like:

CC = gcc
CFLAGS = -W -Wall -O3 -mno-cygwin -std=c89
-mms-bitfields -std=c89
LDFLAGS = -s -mwindows
TARGET = something.exe
OBJ = something.o Rsrc.o
all: $(TARGET)
Rsrc.o: Rsrc.rc
	windres -o Rsrc.o Rsrc.rc
$(TARGET): $(OBJ)
	$(CC) $(LDFLAGS) -o $(TARGET) $(OBJ)
Rsrc.o: Rsrc.rc
something.o: something.c something.h

Or there are many ways. But important here is that make.txt, as that command redirects the error messages there. And here comes onother thing the vim is useful for, write :cfile make.txt, and then every :cnext brings you to …