Creating dll in C from source and heading files - c

How can I create dll from separate source and heading files (written in C).
I have
extrfunc.h
tricclib.c
tricclib.def
tricclibql.c
And I need to create dll (using c not c++) from these components.
These files are just here:
https://drive.google.com/drive/folders/1EyvxHxiOLJqNp7sZwn0YOsOT2eRJLCL1?usp=sharing
Thank you!

That depends on what compiler you're using. Because you are calling it a DLL, I am assuming you are running Windows. When I compile on Windows I use the MinGW port of GCC. Assuming this, do the following:
Make sure you have the correct __declspec on your functions.
Compile each source file to an object file with commands like: gcc -c -o example.o example.c
Link it with something like gcc -shared -o output_dll.dll object1.o object2.o -Wl,--out-implib,libexample_dll.a

Related

Link C against object from LLVM

I've been playing with LLVM more and ran into a wall trying to do this:
Assuming I have the bitcode of a project (input.bc), I can compile that to an object file (input.o) using llc.
Now if I write a separate file (funcdefs.c) that uses some symbol definitions which are in input.o, is it possible to compile funcdefs.c to its own IR representation using input.o as an include?
I've tried clang -c -emit-llvm input.o funcdefs.c but I don't see anything that looks like it's finding the missing symbol/global definitions.
Thank you for any guidance!
You need to create an object file for funcdefs.c first and then link it with input.o.
clang -c funcdefs.c -o funcdefs.o
clang funcdefs.o input.o
You can club those into
clang funcdefs.c input.o
To use functions from input.o file you need to create input.h file with definition of functions in it. To create such file see How to call the functions in another compiled *.o object files in C and How do I find out what all symbols are exported from a shared object?

Calling functions from an external C file that has its own main()

I have two C files, program.c and tests.c, that each contain a main function.
program.c is a standalone program, that compiles and run normally on its own. But I would like to also be able to use some of its functions in tests.c (without using a common header file). Is there a way of doing this?
If I insert the prototype of the function I want from program.c into tests.c and compile with:
gcc -o program.o -c program.c
gcc -o tests.o -c tests.c
gcc -o tests tests.o program.o
I obtain an error duplicate symbol _main, which I understand since there are indeed two `main' functions.
I basically would like to be able to treat program.c both as a standalone program and as a library, similarly to what could be done in Python with if __name__ == '__main__'.
If you need to have two separate distinct executables for which some of the functionality between them is similar you can share the common functionality by placing relevant functions into a third file, and compiling as a portable executable, DLL in Windows. (or shared library in Linux.) Each of these file types contain sharable, executable code, ithout the main() function, designed to be linked during compile time, and dynamically loaded into your executable at runtime.
Here is a step by step set of instructions for shared library using GCC and Linux.
Here is a step by step example for creating DLL using GCC in windows.
So I managed to achieve what I wanted thanks to the comment from #pmg:
I compile program.c into a standalone binary (gcc -o program program.c), but I also compile it into an object file with "main" renamed (gcc -c -Dmain=mainp -o program.o program.c).
I can then use this object file (that does not contain a "main" symbol anymore) to compile tests.c: gcc -o tests tests.c program.o.
Thanks #pmg, I did not know this use of the -D option.

In C how do I compile and then create an executable file with a header and two c files?

I have three C files in total. One is a header [.h] file, two are source [.c] files.
The .h file is called encryption.h and the corresponding source file is encryption.c. The encryption.c has logic, but no main() function. My second c file is called main.c. There I have the main() function that calls methods from encryption.c.
I am compiling these files within terminal on Mac OSx. I am confused on how to compile this, I have tried the following:
gcc -c main.c
gcc -c encryption.c
gcc -c encryption.h
gcc main.o encryption.o encryption.g.gch -o encrypt
This doesn't seem to work though, it says I have a precompiled-header already. I tried finding the answer online, I know it has to be simple, but I haven't had much luck. What is the issue here?
Don't compile the header file. Header files are meant to be included to the source files (using #include directive, in c). Just compile the source files and link them together. Something like
gcc -c main.c
gcc -c encryption.c
gcc main.o encryption.o -o encrypt
or, for shorthand,
gcc main.c encryption.c -o encrypt
Note: If you're bothered about the presence (or absence) of header files while compilation, check the pre-processed output of each source files using gcc -E option.

Tiny C Compiler (TCC) and winsock?

Can I use a socket library from TCC? I can't find any reference to winsock or sys/socket.h in the include directory.
If i remember correctly, winsock was part of the windows platform SDK (?) If so can I link that with TCC?
According to Tinycc-devel mailing list
you should give this a try:
tiny_impdef winsock.dll -o winsock.def
tcc yourcode.c winsock.def -o yourcode.exe
Use tiny_impdef.exe to export definitions from the DLL file using the command line:
tiny_impdef.exe wsock32.dll -o .\lib\wsock32.def
You will also need the header files for your source code to include them. MinGW's ones (such as winsock2.h, ws2tcpip.h, ws2spi.h...) can be reused with TCC.
The MinGW compiler can be downloaded from here. Just copy the headers you need from MinGW's include directory to TCC's include\winapi directory.
At compilation time, you will need to tell the compiler you are using the Windows socket library:
tcc.exe path\to\code.c -lwsock32 -o path\to\program.exe
tiny_impdef winsock.dll
copy winsock.def to lib/
run:
tcc -lwinsock yourcode.c -o yourcode.exe

Compile multiple C files with make

(I am running Linux Ubuntu 9.10, so the extension for an executable is executablefile.out) I am just getting into modular programming (programming with multiple files) in C and I want to know how to compile multiple files in a single makefile. For example, what would be the makefile to compile these files: main.c, dbAdapter.c, dbAdapter.h? (By the way, If you haven't figured it out yet, the main function is in main.c) Also could someone post a link to the documentation of a makefile?
The links posted are all good. For you particular case you can try this. Essentially all Makefiles follow this pattern. Everything else is shortcuts and macros.
program: main.o dbAdapter.o
gcc -o program main.o dbAdapter.o
main.o: main.c dbAdapter.h
gcc -c main.c
dbAdapter.o dbAdapter.c dbAdapter.h
gcc -c dbAdapter.c
The key thing here is that the Makefile looks at rules sequentially and builds as certain items are needed.
It will first look at program and see that to build program, it needs something called main.o and dbAdapter.o.
It will then find main.o. However, to build main.o, it will need main.c and dbAdapter.h (I assume dbAdapter.h is included in main.c).
It will use those sources to build main.o by compiling it using gcc. The -c indicates the we only want to compile.
It does the same thing with dbAdapter.o. When it has those two object files, it is ready to link them. It uses the gcc compiler for this step as well. The -o indicates that we are creating a file called program.
GNU make should be what you're looking for.

Resources