by jalino23 · 3y ago · 0 comments
Coming from Node/npm I’m used to the idea that your dependencies gets bundled in your app. But its not like that on C.
you expect your users to install your app dependencies for you, and you have to figure out the path of your own dependencies.
cause they don’t live in one nice place like node_modules. and I haven’t even began to figure out how Im supposed to know what linker flag to use for the library I use.
the tutorials just give it to you without explaining where they get it from.
and most of the sources I’ve read almost always recommends dynamic linking cause static is heavy. which brings me back to this article that now recommends static linking and I agree with that.
much of the learning materials I’ve come across are just spoon fed to you without explaining why. And its so incredibly frustrating until you look at linux history, most of the assume knowledge now is based on baggage upon baggage of layers of history.
Yes, you do. And that is a good thing.
> and you have to figure out the path of your own dependencies.
No, you don't. Once a build dependency is installed, the compiler and the linker find it automatically.
All of this is easier than it sounds. For example, say your program depends on libpng. Then your users will have to "apt get install libpng" (or whatever your distro does). Then on your C files you just write #include <png.h>, on the call to the linker you will have -lpng and everything will work correctly.
The C ecosystem does not favor dependencies whose api changes every month, so this will work with whatever version of the libraries you have. Of course, you are also free to bundle all the dependencies that you want with your code. And you can easily distribute static executables that will work everywhere.
Except when they don't.
And you're left to figure out the right incantation of linker flags to use. Maybe give up and start symlinking stuff around until it works.
Hmmm. I'd rather say that some people who package libraries are very annoying! If a library needs specific compiler flags, or linker flags beyond "-lfoo", this means that somebody is doing something wrong. Out of incompetence or out of malice.
There's also a lot of silly memorization involved (e.g. remembering that zlib is usually packaged as `libz` so you need `-lz`, remembering that the non-SSL parts of OpenSSL are `-lcrypto`, etc.). C programmers tend to build mental scar tissue around these things and forget how pointlessly complicated and friction-inducing they are.
I've found it just as much fun as Scheme, my favorite language to work in. If you think of Scheme as the minimum-usable implementation of lambda calculus, then C is like the minimum-usable implementation of a Turing machine. They're kind of dual to one another in a very fun way.
The one thing I'm really struggling with, like you, is understanding the C "ecosystem". Anything past Makefiles is so complex, especially for my uses. Using something header-only library like STB or a simple library like Raylib is super straightforward, but figuring out what, when, and how to use things like pkg-config, Cmake, Meson, or other tools is a real headache. Does anyone have any favorite resources?
I'd also appreciate something more detailed about modern C style, when and why I'd need a custom allocator, or anything else you wouldn't see in K&R or other introduction.
PS A lot of people say things like "libc" sucks, you shouldn't be using XYZ function from libc. What should I be using instead? I'm all for learning via implementing things myself, but surely that's not the recommended route for everything in C world.
Yes. The GNU make documentation. Just don't use cmake and similar stuff. You can typically rewrite your complex, non-working Cmakelists as simple, straightforward Makefiles.
Add `--libs` to link against it, too: -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/sysprof-4 -pthread -lglib-2.0
With meson that is basically `dependency('glib-2.0')` that you add to your executable/library.
> you expect your users to install your app dependencies for you, and you have to figure out the path of your own dependencies.
If you use a good buildsystem, it will find those dependencies automagically or you can e.g. use wrap dependencies: https://mesonbuild.com/Wrap-dependency-system-manual.html Those do automatically download the dependencies and compile them for you
That works sometimes, but is there a general way to find out what to pass to pkg-config besides consulting StackOverflow (or now perhaps ChatGPT)? For example, my recent history looked something like:
sudo apt install libopencv-dev
pkg-config --libs opencv # did not work
pkg-config --libs OpenCV # did not work
pkg-config --libs opencv-core # did not work
pkg-config --libs opencv45 # did not work
pkg-config --libs OpenCV45 # did not work
pkg-config --libs opencv4.5 # did not work
pkg-config --libs OpenCV4.5 # did not work
pkg-config --libs opencv-4.5 # did not work
pkg-config --libs OpenCV-4.5 # did not work
pkg-config --libs opencv-4 # did not work
pkg-config --libs OpenCV-4 # did not work
pkg-config --libs opencv4 # success!Anyway, the typical C program doesn’t pull in a ton of dependencies.
but neither language (and they are very different) has standardised library/package tools.
C++ is a much more complex language than C. The things you need to worry about in C are far fewer than in C++.
While this might be true, generic containers, sane strings, RAII and the possibility of not having to deal with pointers makes C++ an easier language to work with. You certainly need to worry about much less things long as performance is not a priority.
So, explain to me why every C++ program always has its own String implementation?
> long as performance is not a priority.
Then C++ is a disastrous choice. Almost anything is better.
Sure, you have to do more for yourself in C, as it isn't as powerful as C++.
C++ is the only language I've worked with where you have to tell people which bits of the language they can use!
Nuke the site from orbit. It's the only way to be sure. Deprecate C for all greenfield development, and launch a big-science push to rewrite all the extant load-bearing C code in something else with safety guarantees (Rust=good, SPARK=better). If null pointers were a billion dollar mistake, then letting C become ubiquitous was a mistake that runs in the trillions.
speaking as a c and c++ trainer here.
Writing C code is hard.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sysexits.h>
#define die(E,...) ({ \
fprintf(stderr,__VA_ARGS__); \
exit(E); })
void reverse(char* a,size_t z) {
for(; z>=2 ;a++,z-=2) {
char t=*a; *a=a[z-1]; a[z-1]=t; } }
int main(void) {
size_t z=16,i=0;
char *a=malloc(z), *p=0;
for(;;) {
for(;;) {
ssize_t n = read(0,a+i,z-i);
if(n<0) die(EX_IOERR,"input error: %m");
if(n+i==0) exit(0);
if(n==0) die(EX_DATAERR,"error: %zu bytes of garbage at end of file",i);
p = memchr(a,'\n', (i += n) );
if(p) break;
if(i > z/2) {
a = realloc(a, (z = z+z/2) );
if(!a) die(EX_OSERR,"realloc(%zu bytes): %m",z); } }
while(p) {
reverse(a,p-a);
fwrite(a,p-a+1,1,stdout);
memmove(a,p+1, (i -= p-a+1) );
p = memchr(a,'\n',i); } } }
This produces: $ ./a.out
cat
tac
warning: incompatible implicit declaration of built-in function
noitcnuf ni-tliub fo noitaralced ticilpmi elbitapmocni :gninraw
naïve resumé
�muser ev�an
> i guarantee the c++ code will be easier to write and understand than any c code.Put up or shut up, as the saying goes. At the very least it ought to be easy to abstract out some of the memory allocation and file handling to use C++ builtins, and I'd be interested to see what other improvements you can manage.
Edit: 0: Exercise for the reader: consider the input "abXc", where 'X' is some as-yet-undefined codepoint that will, at some point in the future, be defined as either a combining diacritic, or a standalone character. If 'X' is a character, the output should be "cXba", but if it's a diacritic, the output should be "cbXa". The information about which (mutually-exclusive) output is correct does not exist yet.
#include <stdio.h>
#include <stdlib.h>
int main() {
size_t len = 0, cap = 0;
char *buffer = NULL;
int c;
while ((c = getchar()) != EOF) {
if (len >= cap) {
cap = (cap << 1) + 1;
{
char *newbuf = realloc(buffer, cap);
if (!newbuf) {
fprintf(stderr, "Could not reallocate to %zu. Printing what I've got.", cap);
break;
}
buffer = newbuf;
}
}
buffer[len++] = (char)c;
}
while (len --> 0) {
if (putchar(buffer[len]) == EOF) {
fprintf(stderr, "Output failure.");
return 1;
}
}
free(buffer);
return 0;
}
Most of the verbosity here is in correctly handling errors, something that isn't much easier to do in C++. (Be warned: I haven't tested this code.)That will depend on how you define "understand". For a superficial level of understanding, you might be right. But the C++ code will make use of many concepts that would take a while to explain, for example references, classes, inheritance, templates, operator overloading, STL, and probably a bunch of other things I forgot.