Generating dll file from object files - c++

When I want to generate a shared object (.so) in unix os from object files, I simply enter following command:
g++ -shared xxx.o yyy.o zzz.o -o module.so
I am wondering if I can do the same thing in windows in order to generate a .dll file from object files. Is that possible?

If you have MinGW32 or Cygwin installed, then yes. But you can't do it using Visual Studio directly, since Visual Studio uses the cl compiler driver which has an entirely different set of options.

Related

creating a project on mingw under windows

I am creating a project using mingw under windows. When run on another PC (without wingw in PATH, respectively), get errors, missing: libwinpthread-1.dll, libgcc_s_seh-1.dll, libstdc++-6.dll. How can I build the project so that everything starts up normally?
A C++ program needs runtime libraries to run. They're usually not part of the program itself! So you'd need to ship these libraries alongside with your program (which is what most software does).
You can, for many things, however, also use "static linking", which means that the parts of the libraries used by your program are included in your program itself! The -static flag supplied to executable-generating step in your project will do that. If your program consists of but a single file, that would be g++ -o test -static test.c (your g++ might be called x86_64-w64-mingw32-g++ or so).
If your distributed .exe file won't run because of missing .dll files that's because it is linked against those files and you need to distribute these files with your .exe file.
But actually there are 2 solutions to this problem:
Shared build (= using .dll files): distribute the .dll files with your application. The best place to put the .dll files is in the same folder as the .exe file(s). You can use Dependency Walker to figure out which .dll files your .exe file needs (and which .dll files those .dll files need etc.) or you can use the command line tool copypedeps -r from the pedeps project to copy your .exe file(s) along with any .dll files required.
Static build (= everything rolled into the .exe file(s)): when building your .exe file(s) use the --static linker flag (and if needed also -static-libgcc and/or -static-libstdc++). This create a static .exe file, which means all the static libraries are rolled into the .exe file. This may make your .exe quite large (though you can try reducing its size with the strip command or the -s linker flag) and doesn't have the advantage of .dll files where common code is only distributed once. It may also cause longer loading times for your application, since everything is loaded at once.

CPP codes linking in VScode with Mingw-w64 in Windows 10

I'm learning C++ with VScode with Mingw-w64 in Windows 10.
Today, I watched how to make header files and build the separated codes in Visual Studio 2019.
I wanted to implement this process in Visual Studio Code editor, but I met linking error.
My naive solution is like
~$ g++ -c .\main.cpp .\add.cpp
~$ g++ .\main.o .\add.o -o test.exe
First, compiling the source codes into object files.
Second, linking those object files to an execution file.
It works anyway, but when I tried to run the debugging mode, it doesn't work.
I think I need to edit launch.json file, but I have no idea...
The following images are about my situation.
How can I build those separated codes at once?
In this time, I just described the path of the definition code into 'tasks.json' file.
After then, built the main code again, so that it worked.

Manually compile QT project with g++

I am currently working on a polymorphic engine that works for c++ projects. What i am doing is that i am
(1) generating assembly files from .cpp files
(2) generating object files from assembly files
(3) loading object files into an .exe
All this is done using the g++ compiler through the command line. Now, all this works fine for a regular win32 c++ console application project created in visual studio, but i also need to adapt my polymorph engine to be able to build QT projects (GUI) with the same method of approach listed above.
I am unable to find commands to replicate this for QT projects. Below is the commands i am currently using in order to manually compile a console application project built in Visual studio.
(1) g++ -S -masm=intel -o my_asm_output.s main.cpp
// generate assembly from main.cpp file
(2) g++ -c -o my_obj_output.o assembly.s
// generate object file from assembly.s
(3) g++ -o my_exe_output.exe my_obj_file.o
// generate executable by loading object file into it
Thanks!

Linking a .lib file on Windows 7

I have a C++ program test.cpp and I want to link two .lib files to it(fhlib.lib and gc_lib.lib).I have the .lib files in the same folder as my .cpp program. I'm on Windows 7.
What I have tried so far is the following:
g++ -o main main.cpp -L/Users\Documents\Visual Studio 2015\Projects\My Project -lfhlib
But I get an
No such file or directory error.
I'm sure the path is correct because I copied it from Properties->Location. But I had do delete the "C:\", because it was not compiling.
EDIT: I found this http://www.mingw.org/wiki/Specify_the_libraries_for_the_linker_to_use.
So I tried using
"-I" instead of "-l"
But still doesn't work.I get:
undefined reference to 'fh_set'...
If you're compiling with g++ on windows, I guess you're using MinGW: MinGW relies on .a libraries. When using the "-l" option, the compiler is looking for a library file with the extension .a.
Libraries in the format .lib are compiled with visual studio: you can't use it as this. Compile your libraries with MinGW if you have the sources or consider migrating your project to visual studio.
So the problem was that the lib files where compiled in VS. And I had to use the VS compiler instead of g++ and everything worked fine.

How to output preprocessed code AND compile it (Visual Studio)

I'm generating preprocessor output (.i) from Visual Studio, but also want to do the actual build. Is there a combination of flags that will both output the .i file without then stopping the compiler from going ahead with the build as normal?
This is currently just C++ but will probably want to use this with CUDA later, so prefer answers that work within Visual Studio rather than require command line (unless it works for CUDA too).
The point of this is to save the time it takes to do Project->Properties->Config Props->C/C++->Preprocessor->Generate Preprocessed File Yes/No plus Rebuild. Particularly it is irksome to me that the preprocessor has to run twice, so a solution that somehow generates the .i file in part 1 and then compiles that file in part 2 would be fine.
The exact version of Visual Studio I'm using is VS 2008 Express
You can add a custom build step or a new target to dump the preprocess after the code is built by definition the dumped preprocessor output is what is built.
You can create a custom build config that does pre-processing and then define a batch build that builds the pre-processed version followed by the actual compiled / linked version.
Use the switch /P Example- cl /C sample.c
The above will generate a .I file with same name (sample.I). Now to compile the .I file, just rename .I file to .C then do cl /c to compile and generate an object file.
If your running GNU gcc/g++ compiler, then configure a Makefile target "recipe" with the following code
gcc -save-temps -dumpbase save-foo -c foo.c
Reference can be found in the terminal man pages for gcc.
man gcc
section:
-dumpbase dumpbase
This will create a separate .i file and compile "foo" to object code. That code can then be used as a prerequisite for your target "goal" executable. Link below explains the vocabulary I used.
https://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html#Simple-Makefile