I am attempting to get an understanding of how building and linking works, since there's a library I want to compile into a DLL. I do not have control over the tool that will be used to compile - It's going to be the msvc tools invoked via command line.
To that end, I've managed to locate a tutorial on creating DLLs that does not use Visual Studio. it's for mingw, but I've managed to make it work with cl
I've managed to translate part of the process to use with the msvc tools:
gcc -c -o add.o add.c -D ADD_EXPORTS
was translated into
cl /c /D ADD_EXPORTS add.c
I was unable to fully translate the line that follows:
gcc -o add.dll add.o -s -shared -Wl,--subsystem,windows
But the following worked:
link /DLL /NOENTRY add.obj
and created .exp, .lib, and .dll files. The -s switch doesn't seem important (it strips some info, so I'm guessing at worst I'll get a slightly larger file)
I then managed to create a .obj file from addtest.c, link it (against the .lib file), and got an .exe that runs perfectly fine as long as the dll is keeping it company in the same folder.
On to a harder problem: The library I ultimately want to have compiled
after playing around with the #includes and moving some files around to make sure everyone can find their header, I compiled each source file (for windows - to my understanding, _WIN32 is automatically defined when preprocessing) the same way I did add.c (sans ADD_EXPORTS), plus a file of my own, called Source.cpp (I did not create a header for it)
#include "serial/serial.h"
#include <combaseapi.h>
using serial::Serial;
using std::string;
using std::vector;
using serial::PortInfo;
Serial* sp = NULL;
extern "C" {
__declspec(dllexport) bool __cdecl createConnection(const char* port, uint32_t baudrate)
{
try {
sp = new Serial(port, baudrate);
return true;
}
catch (...)
{
return false;
}
}
}
After making a .obj file of them all, I attempted to invoke the linker, with all of the .obj files I've generated, using link /DLL /NOENTRY Source.obj list_ports/list_ports_win.obj win.obj serial.obj
for which I got 134 errors
Information on how to create a DLL without visual studio seems to be quite scarce, and I can't find anything useful about the errors I've looked up (no common mistake I could find when looking up the error + symbol)
What's the problem here?
Edit: Thanks to a comment I managed to find out I have a problem linking to CRT libraries. This helped me cut down on the number of errors, I will see if I can figure anything out about the new output I get and edit further
new linking commands:
link /DLL /NOENTRY Source.obj list_ports/list_ports_win.obj win.obj serial.obj /LIBPATH:"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Redist/MSVC/14.29.30036/x64/Microsoft.VC142.CRT"
Edit 2:
compiling the source files with cl /c /clr source_file led to significantly fewer errors when linking without specifying the LIBPATH. I think I might even be able to resolve all those by myself. I will continue to work on this later.
