r/C_Programming • • 6h ago

Question Hi! I am using visual studio but it doesn't work. error: "missing filename after '-o'" what should i do?

0 Upvotes

r/C_Programming • • 12h ago

Four Years of development in C

Thumbnail
github.com
1 Upvotes

Exactly four years ago, I started working on BouncyCastle - a cryptographic key storage simulator with a PKCS#11 interface, a REST API, and a web management interface. It is designed for developers working with HSMs and smart cards, as well as for QA environments.

At the time development of SoftHSMv2 (C++) began, it was practically dead (in 2022), and bugs weren’t even being fixed, moreover, it had architectural issues (SIGSEV if you used the application with the wrong version of OpenSSL, problems with multi-user access, poor logging, poor cross-platform compatibility, and support for different algorithms on different platforms).

Note: Kryoptic (Rust) was created later, but it merely ported all of SoftHSMv2’s problems from C++ to Rust.

BouncyHsm has a native PKCS#11 library in C (exporting over 100 functions) and a backend written in C# where cryptographic operations are performed (supporting 210 mechanisms) - one could say, with a bit of hyperbole, that it is a network HSM.

Why I chose C for the native PKCS#11 library. I considered several options and even experimented with them:

  • C# - starting with .NET 7, it can create native libraries, but I was worried that the garbage collector would bloat the binary (a simple REST client was 8 MB, in later versions, it was only about half a megabyte),
  • Rust - while experimenting with Rust, I realized I’d end up with a huge amount of unsafe code and exceptions for different platforms. I was also concerned about the dependencies that the libraries would introduce,
  • Zig - it’s still not at version 1.0.

Ultimately, my experiments showed that pure C would be best for this project, without any external dependencies, using Messagepack for RPC (via the one-file library cmp) and TCP via the operating systems API. One of the specific requirements I set for myself was that the PKCS#11 library must not have any external dependencies to prevent crashes. Another advantage is that you can even compile it for your "refrigerator or toothbrush".

What I've learned from developing in C over the past 4 years, and what I would do differently:

  • I've come to love the WinAPI - yes, it sounds strange, but low-level programming on Windows is a million times better than on UNIX systems. Because theres an API for everything, on Linux, I had to parse a lot of things from text files,but be careful - some Linux distributions have them in a different directory, and some use a different format. It was a pain.
  • glibc - and its various versions on Linux - be damn careful with this.
  • be careful with online tutorials, because they’ll recommend code that uses extensions that only work in gcc/clang.
  • instead of one big Makefile, I’ve found it works best to have several small ones - one for Linux and one for macOS (I also have one for MinGW, but on Windows I use MSVC).
  • For a long time, I resisted using goto (goto clean/goto error) and tried to write code without it; in hindsight, I regret that, because it complicated memory management for me (I’m waiting for the C2Y standard and defer).
  • It’s a good idea to have your code checked by AI, it can find silly mistakes like memory leaks.
  • Don’t trust AI - it’ll suggest things it’s seen on forums, even if they aren’t part of the standard.
  • When you start an open-source project, people will want you to fix bugs in it.

P.S.: This was originally supposed to be a short post, but it kind of got out of hand. If you have any questions, feel free to ask.


r/C_Programming • • 1h ago

Discussion Why did C++ crush Objective-C in popularity?

12 Upvotes

I know C and C++ are largely source-compatible with each other, but Objective-C is specifically a strict superset of C, not just a fork of it, like C++. You’d think that scratching that OOP itch while also giving you complete source compatibility with C would cause more companies or projects to utilize it, at least in some respect, but that may just be a naïve prediction. What does C++ have that Objective-C doesn’t that you think caused such a large popularity gap between the two languages? Am I missing some nuance?


r/C_Programming • • 47m ago

Question What happened to Muukid libraries?

1 Upvotes

I am trying to find some C libraries created by a developer named Muukid.

He created muDOG, muCOSA, and muGraphics. He also had a YouTube channel where he showcased his libraries in hour-long videos, but it seems the channel has been deleted. The links to the channel no longer work, and his GitHub page also appears to have vanished.

Does anyone know if these projects are still available anywhere? I am particularly interested in finding archived copies of muDOG and muCOSA, as I was only able to clone muGraphics.

Any information would be greatly appreciated.


r/C_Programming • • 20h ago

Best Way to Read 'Raw Data'

17 Upvotes

I'm writing a C program for the pi pico and realized I never really figured out how to properly cast characters to raw bytes then to other things, or if there was really a way to do that.

  1. Does char mean "signed char" by default in any C program, or is that up to interpretation by the compiler?
  2. Would using a void pointer to dereference then recast to *uint8_t work?
  3. When bytes are read through getc(), are the bytes modified before they are stored? As in, how are the bytes that are read interpreted to translate to characters, and how are they stored? My initial thought was that the bytes were interpreted "as-is", meaning that they were copied directly to the memory in the character and interpreted as signed characters. But it seems like they're being reinterpreted in some way as they are cast.
  4. If something about the process is too implementation specific, is reading as "raw data" just a flat out bad idea or not supported at all? Will the behavior be undefined on other systems? As long as I can get it to work on the pico it's fine, it doesn't have to be portable. But for future reference, should I not try to think of things as "raw data" unless the implementation specifically defines how it stores the data?
  5. Less important, but how are ascii codes mapped to signed characters? (every chart I've seen has never listed a negative number, only 0-255) (( and if signed is the standard, then why does every asii chart list 0-255... ))

// this was the void pointer way I was going to try, would this work?

tnext =  *((uint8_t*)((void*)&next));  // Cast to uint8_t

(Not sure if this is a repeat but I couldn't find exactly what I was looking for. Sorry if this is a dupe)

Conclusion: Seems like when you read getc() you just need to indicate "unsigned char" so that the byte is read unmodified and can be cast directly to a "uint8_t"? I think the reason I hadn't done that originally was because I thought "but getc returns char, not unsigned char", but I dont think that matters. getc should read into either


r/C_Programming • • 10h ago

cc_list: an intrusive doubly linked list, faster than Linux kernel's list_head and BSD TAILQ

Thumbnail
github.com
2 Upvotes

cc_list is cc_task (async/await closure coroutine library in C) 's linked list implementation.

It's node points directly to the next container, not next node; so you don't need to do `container_of` to get the container.

Linux kernel's list_head is classic, it's node pointers to next node, perform container_of, just a sub CPU instruction, to get the container.

cc_list optimized away this sub instruction so it's even faster than linux's list_head!
And using cc_list is just similar like using list_head.

The head is not a container, just a node with prev/next pointers. The trick is when empty, head points to a non-existed shadow container, just point but no dereference, so it's safe.

┌─────────────────────────────────────────────┐
└►........... ┌─►┌─────────┐ ┌─►┌─────────┐   │
  :  shadow : │  │container│ │  │container│   │
  :         : │  │         │ │  │         │   │
  :         : │  │         │ │  │         │   │
  ├─────────┤ │  ├─────────┤ │  ├─────────┤   │
  │   head  ├─┘  │   node  │─┘  │   node  │───┘
  ├─────────┤    ├─────────┤    ├─────────┤
  ...........    └─────────┘    └─────────┘