#Compile C code to DLL

33 messages · Page 1 of 1 (latest)

full girder
#

Hello, I am somewhat of a beginner to C and was wondering how I can compile my code that utilizes SDL2 and SDL2_ttf for a simple GUI library that I can use. I was wondering if anyone could help/give me advice for how to do this, as I have no idea where to start.

flat quarryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

full girder
#

I have a bunch of different files and chatgpt has given me no good advice for where to start from. Along with that I can't find any good tutorials on it either.

urban viper
#

What compiler and os?

full girder
urban viper
#

Add the -shared linker flag: g++ 1.cpp 2.cpp -shared -o libMyGuiLibrary.dll

#
#ifdef _WIN32
#ifdef MY_LIBRARY_BUILD
#define MY_LIBRARY_EXPORT __declspec(dllexport)
#else
#define MY_LIBRARY_EXPORT __declspec(dllimport)
#endif
#else
#define MY_LIBRARY_EXPORT __attribute__((__visibility__("default")))
#endif
#

Also need to add something like this to your library header, and mark every public function declaration with this macro: MY_LIBRARY_EXPORT void foo();

#

Then when building the library, add -DMY_LIBRARY_BUILD to make it expand to dllexport. When using the library you want it to expand to dllimport

calm dove
#

Install the SDL library from the Official website or you could get it on GitHub, extract the files then add the include file of the downloaded file to the include folder of MinGW. You'll be set to use the it then.

full girder
#

so the structure looks like this, for each file your saying i need to prefix each function declaration with some sort of name for the library?

#

and then i still dont understand what i do from there...

urban viper
#

It's easy to lose tract of what you copied where

urban viper
full girder
#

im so lost now...

full girder
#

i just dont like even know what im doing lol

gilded lantern
# full girder im so lost now...

if i understand it correctly, __declspec(dllexport) is a function attribute used by windows to declare that "this function is in a DLL and must be presented as such". the -shared flag tells gcc to compile it as a library; you need both the flag and the function attribute because the code is windows and the compiler is OS agnostic. if you're using the library (most likely when you include the headers in a project), then you need to tell windows that you're importing the functions from a DLL -- this is what __declspec(dllimport) is for. a brief explanation of function attributes: they're used to tell the compiler things that are not part of the functions type (the return type and types of the parameter list are part of a function's type). this includes things like noreturn for functions that exit the program (and hence do not return), deprecated for functions that are kept around for compatibility reasons but should not be used, gnu::hot for functions compiled with gcc that you wish to inform the compiler are going to be called often . . . in short, all information not necessary for the caller of the function to know, but which you wish to pass to the compiler anyways. the syntax in C23 isc [[deprecated]] void foo(); earlier incarnations of function attributes were always compiler extensions and so varied in their exact syntax, but pertinant to this discussionc __declspec(dllimport) void foo(); would be the way you use it. the #ifdefs are just syntax sugar meant to make it easy to change one thing in one place and thereby change the __declspec(dllexport) to __declspec(dllimport) easily for the header file #includes

full girder
#

Ok. I get that part of it, its more how to implement it into my code because I have so many files and headers and all that. From what I understand from previous responses, I create a main.c file which has #ifdef _WIN32 #ifdef MY_LIBRARY_BUILD #define MY_LIBRARY_EXPORT __declspec(dllexport) #else #define MY_LIBRARY_EXPORT __declspec(dllimport) #endif #else #define MY_LIBRARY_EXPORT __attribute__((__visibility__("default"))) #endif
and from there I prefix all of my functions with MY_LIBRARY_EXPORT or whatever I set there and I am good?

gilded lantern
#

if i understood it correctly, that should go at the top of your header file, but the rest is correct

full girder
#

but then i get conflicting information saying I must have this in a header file and instead in each header file in the program which has functions i must put this and for each function in header file and c file i add that MY_LIBRARY_EXPORT tag?

#

im not sure becuase both ways I tried it compiled to a dll without errors but then when i ran it it gave me like 1k+ errors saying random stuff that i dont understand. I also tried to look that the dll using objdump -p mylib.dll and it shows all the functions as there. It just sends errors like it wants to im not sure what i need to do lol.

#

I have the repository if you would like to look at it if you know what your doing... its super sloppy though, and I'm sure you don't really want to lol.

gilded lantern
#

please, by all means, sendcode

full girder
#

very sloppy and its my first C program so don't hate on the code unless there is something really bad lol...

gilded lantern
#

so at the top of all your .c files that get compiled to the DLL, just below the #define SDL_MAIN_HANDLED, add #define BUILDDLL, and i think that should be all you're missing

full girder
#

Alright, I’ll try that and see.

#

idk even know anymore because now its giving me SDL errors as well lol

#

i think what im gonna try to do is copy all of the code over to a new repo and see if i can fix SDL there, becuase it just broke in the current one.

#

I tried to switch something with my SDL files a bit ago and it broke it today for some reason i guess.

urban viper