How to compile C programs

AI image. You aren't really missing anything if you can't see it.
An AI image of the time when that code compiled.

First, you try to avoid it. Check for a software package from your distro. If you can't find one, maybe there is an appimage? No? Why? Is the software too old? Is it ancient software from AT&T UNIX or from BSD? Do you really need it? Are you sure? If you haven't given up yet, you should.

You are still here? Ok then. Does the software have a "configure" script? It will be a file called "configure". If so, try this:

./configure --prefix=~/opt/foo
make
make install

If that works, then you are all set. But it probably didn't work. You will see some error message. If you don't understand the error message. That's ok. Ask the Google or ask the AI. The most likely thing is that you are missing some code library. For this example, "foolib" or "libfoo". On Ubuntu, search for this using Synaptic. You will be looking for a software package that has "-dev" at the end, like "foolib-dev" or "libfoo-dev", install it. If you are using Fedora, ask the AI. If you are using Arch Linux, you are just making things difficult for yourself. If you are using Slackware, you are probably just reading this for the laughs. Now try the configure script again.

Ok, you are still reading. This means that it didn't work, huh? What happened? They didn't have the right version of the library? They didn't. This means that you have to get the missing library source code, and then start right back at the beginning trying to compile that. There can be quite a few missing libraries. So this part can take forever. I would give up. Because I have wisdom painfully earned from doing this type of thing many times.

Not wise yet? Ok. Suppose you have all of the missing libraries or maybe you didn't need any, but it still won't compile. The problem is the Makefile, ain't it? Maybe check out the blog post here: How to make 'make' make . See if that helps. No? Well then forget the Makefile. Erase it. Really. You are going to make an AMALGAM!

Sqlite, one of the most popular embedded databases ever, can be obtained as a single C source code file. They call this an amalgam. It is basically all of the source code files concatenated into one. It is the best thing ever. It compiles easily. Do you remember all of those stupid compiler errors? Remember how the computer can't find its stupid files. Are computers even smart? Well, with an amalgam, there is only one file so the computer might just be able to find it.

You need to concatenate all of the source code files into one file. Header files first. Here is an example with a library (foolib) and a program (foo):

cat foolib.h foo.h foolib.c foo.c > amalgam.c

The ordering should be header files, library sources, then finally the main program file (the file that has the "main" function). If you have multiple library files or auxiliary code files, there might be an optimal ordering, but don't worry about that. I'll show you how to add function prototypes to make it work anyway.

Now that you have the file amalgam.c, open it in an editor. You won't need include directives for the files that you just combined. Comment out any include directives that reference these files (these are likely the ones that have quotation marks around the filename). It might happen that an include directive erroneously uses the "<>" thingies for a local file. If so, comment it out. Use your judgement.

Try compiling:

gcc amalgam.c -o foo

If you get errors about undefined functions that you know are there, then you need function prototypes. A function prototype is the first part of a function, maybe the first line even. It is everything up to the first squiggle "{" .

For example:

int bar(char* c) {
... blah blah blah ...

The prototype is the first part of the function with a semicolon behind it:

int bar(char* c);

Solve the undefined function errors by putting function prototypes near the beginning of the file. Where in the beginning? Anywhere after data type definitions. Ideally, the amalgam file will have the datatype definitions in the header files, so function prototypes can go after the end of the last header file. The only real requirement is that the prototype needs to be before any function that calls the prototypical function. So, after the headers is usually good.

You should be able to compile the amalgam at this point, but there are a few complications that I will cover just in case.

What about nonstandard include files and libraries?

If you have nonstandard include files (header files), that the stupid computer can't find, you will have to specify their location to gcc. Use the -I option like this:

gcc -Ipathtoincludefiles amalgam.c -o foo
// or maybe something like this:
gcc -I/usr/include/foolib amalgam.c -o foo

For library files (.a files like foolib.a), the easiest way is to copy them to the current directory. You know that this is the easiest and best way because the nerds will tell you not to do it even though it works. Then you can tell gcc (which will tell the linker) to look for libraries in the current directory by using the "-L" option.

gcc amalgam.c -o foo -L. -lfoo

Be sure that there isn't a space separating the "-L" from the filepath or the "-l" from the library name. Ok. I'm not sure that you can see it because of the font, but I am talking about "L", both capital "L" for and lowercase "L". The option with the uppercase "L" needs the path to the directory that contains the .a file. The option with the lowercase "L" needs the name of the library with the "lib" part removed from the end. foolib becomes foo.

It almost always works!

The amalgam technique solves problems of circular source code dependencies redefinition of variables, and the problem where the stupid computer can't find the stupid files. Try it and leave a comment down below.

So I did this and it changed my life.

Comments:

๐Ÿ”ฅ@justin3902 1 minute ago.
How dare you!

๐Ÿ’ฏ@cafe2277 1 minute ago.
Pfft! Can't compile? Skill issue.

๐Ÿงด@tabitha4152 2 minutes ago.
I am Prince Mugatu. The authorities have seized my priceless beanie baby collection. I would be very grateful for assistance. $$Reward$$ Contact me at Mugat2@scamapp.scam

๐ŸŒ@codesmythe4231 3 minutes ago.
Makefiles are sacred!

๐Ÿ†@lewddude77 5 minutes ago.
My video is better. See more of me at ********.com

๐Ÿ›ต@justin9026 8 minutes ago.
You sir, are no sqlite!