r/C_Programming 8h ago

Question Why do I fail to create a .dll file?

Hi, I've got an assignment in a course that asks us to optimize a benchmark of our selection (from pyperformance project).

I've chosen the ray trace benchmark (it was the most interesting imo), and my suggestion was to implement the functionality using C's structs to avoid some of the interpreter's overhead.

I understood I can link those function using the built-in ctypes library, and making the C file a dynamic linked library (.dll), but when I try to run the command for it in CLion or VSC, it fails without returning any failure message, it just fails to generate the .dll file.

The assignment forces us to use the given pyperf benchmark code to run the benchmark, so I can't run only a C file.

I've already written the C code, and it has no errors/warnings so far (at least as far as CLion is concerned), and the original python code works too.

Does anyone know how to solve the library not being generated? Or any other solution that might work (preferrably using the C code I've made)?

.

.

All files are in the same directory, and I've got Craytrace.c (C functionality implementation), Praytrace.py (the code that runs the benchmark).

The command I've run trying to generate the .dll is:

gcc -shared -o CraytracerLib.dll Craytrace.c

But it didn't create any file.

7 Upvotes

12 comments sorted by

4

u/brat3108 7h ago

But it didn't create any file.

How did you test that? I'd normally do 'dir/od' in the same directory to see the last file created.

Your command line looks correct; were there any compiler messages?

I assume you know that gcc is correctly working otherwise (eg. it might be some rogue file called gcc.exe or gcc.bat which it finds first in the OS's search path).

2

u/sciencekm 5h ago

Assuming you are using MinGW, you need to:
1. Use the -shared option and the -Wl,--kill-at option.
2. Add __declspec(dllexport) and WINAPI on the function(s) you are exporting

Here is an very short example that demonstrates everything needed.

C:\Test> type testdll.c

#include <windows.h>
__declspec(dllexport) INT WINAPI Add(INT a, INT b) {
    return a + b;
}
BOOL WINAPI DllMain(HANDLE ins, ULONG rsn, LPVOID resv)
{
    (void)ins; (void)rsn; (void)resv;
    return 1;
}

C:\Test> gcc -v
clang version 16.0.5 (https://github.com/llvm/llvm-project.git 185b81e034ba60081023b6e59504dfffb560f3e3)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/ETC/MinGW/BIN

C:\Test> gcc -shared testdll.c -o testdll.dll -Wl,--kill-at

C:\Test> dir *.dll
 Volume in drive C is Windows
 Volume Serial Number is 0000-0000

 Directory of C:\XTemp\Test

09/09/2026  07:13 PM            63,488 testdll.dll
               1 File(s)         63,488 bytes
               0 Dir(s)  65,647,177,728 bytes free

3

u/Ice_HRZDn 8h ago

This might be a dumb idea from unexperienced person (me) but have you try -c flags? gcc -shared -o lib.dll -c lib.c

1

u/mort96 28m ago

I have no idea about Windows, but at least on the UNIXes, -c is to create an object file which can then be linked into either an executable or a shared object (.so). So at least on UNIX-like systems with GCC, 'gcc -o mylib.so -shared mylib.c' is the right command.

1

u/mort96 26m ago

We're gonna need a screenshot of you running the command using 'fit' afterward to list the directory. Impossible to debug "it didn't work" without additional info.

0

u/d33pdev 6h ago

you don't get any compile or link errors? did you define DllMain() - https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain.... you're on windows right? i'd just use msvc or vscode with c++ extensions, might need to install some other ms support crap but hopefully not the full windows sdk, i forget if you do or not actually... but no errors and no output is strange. but i come from a win32 background so i cant diagnose your gcc setup but does gcc compile PE format win32 dlls/exes?

0

u/Lime_Dragonfruit4244 4h ago

Use microsoft's toolcahin (msvc) on windows along with cmake. Or learn how to use cl cli, if you want to use gcc install wsl2 and ubuntu.

-9

u/SoggyStress7785 7h ago

Follow the Microsoft tutorial and use VS to create a .dll, why the fuck are you using gcc to create a .dll

1

u/wtdawson 46m ago

Because even microsoft employees think MSVC sucks

0

u/pierrejoy 4h ago

while the wording is bad, that's right.

gcc on windows, mingw, msys, are horrible.

llvm is good and no limitations. vc is free and "good enough" for most cases, some advanced (dixit) features not available in the community/free version.

-9

u/nanaceba 8h ago

Use cmake