1

I have 2 sets of C++ files, one set which I only have the .o files from GCC but no source code, while the other set in Microsoft Visual Studio 2015 which I have the full source code - this set includes various VS libraries such as the Windows SDK. For deployment I would prefer to have one single EXE only (no DLLs), so I would like to link the .o files to the .obj files generated by Microsoft Visual Studio. The .o format seems incompatible with the .obj format, neither one can be converted to the other, and no linker seems to be capable of linking these two types of files together to produce 1 single EXE. Hence, my only option is to compile the set of files under Microsoft VS into .o format. I tried using GCC but even after quite some modifications to the Microsoft library source codes GCC still won't compile - there are a lot of C++ templates that the GCC compiler rejects. This would be too difficult for me to figure out how to modify.

(Edit: statement above seems not completely correct, I missed this earlier, Stack Overflow suggested to me, .o files and .obj files can be converted between them although I have yet to test out the tools and would need more research to determine how reliable this approach is: Converting C++ object file from linux .o to Windows .obj. I believe the .o files from GCC in Windows are the same as those it generates in Linux. Also, I was not clearer earlier, that although the files are C++, the intercommunication between the 2 sets, the VS set and the non-VS set, only needs to be done in C code i.e. .obj will communicate with .obj files in C++ but will communicate with .o files only using C calls. Still, given this approach seems not widely used, I would still prefer compiling VS source files with GCC since as long as I understand the modifications needed for the source files, I can be confident that the end result is reliable.)

I looked into Clang to compile to .o, but the Clang websites seems to indicate that it cannot compile all of the Microsoft Windows libraries either: http://llvm.org/docs/GettingStartedVS.html

I looked into the Intel C++ compiler too, which can compile Microsoft libraries, but I couldn't figure out how to make it output .o instead of .obj under Windows.

Are there any guides on how to modify the Microsoft libraries (included in VS2015) to be compiled under GCC/Clang? The closest I have found online is this: http://www.transmissionzero.co.uk/computing/win32-apps-with-mingw/, which I tested to work on MinGW, but it uses MinGW's own set of (modified?) Windows libraries which excludes all of the ATL files.

(Edit: after exploring a bit more, not 100% sure but quite convinced GCC probably cannot compile MS VS libraries, might just have to wait for a later version of Clang instead. Syntax issues can be fixed, like changing i64 suffixes to LL, getting rid of throw(...) in function declarations, etc., but things like __ptr32, __ptr64, __vectorcall, missing in GCC, not really sure if can be just dummy-ed out, and things like __getcallerseflags seems quite likely no simple workaround, if at all possible. Pasting my GCC command line switches here in case anyone wants to try: -std="gnu++14" -nostdinc -Wmissing-include-dirs -fpermissive -fms-extensions -D "__ptr64 =" -D "__ptr32 =" -D "__vectorcall = __fastcall" -D "__unaligned = " -D "__forceinline = inline" -D "__nullptr = nullptr" -D "__pragstr(X) = _Pragma( #X )" -D "__pragma(X) = __pragstr(X)" -D _M_AMD64 -D _M_X64 -D _WIN64 -D "_MSC_VER = 1400" -D "__int64 = long long" -D _WINSOCK_DEPRECATED_NO_WARNINGS .... -I include the MS VS folders)

Community
  • 1
  • 1
  • the `gcc` compiler is for compiling C code, not C++ code. To compile C++ code use the `g++` compiler. There are two different steps to producing a executable file. 1) compile all the source files into object (.o) files. 2) link the object files, along with any needed library files into the executable file. In g++, (C++) function names are 'modified' by the parameter and return types. (this includes the function names in the libraries.) so the two kinds of object file outputs are not interchangeable. To get more help, list all the file names and how created – user3629249 Oct 15 '15 at 15:38
  • to have only a single executable file, with no dependence on an .dll files, the link step must include the `-static` parameter. Note: using the '-static' parameter to the linker will greatly bloat the size of the resulting executable as all the needed library/.dll functions will be included in the executable. – user3629249 Oct 15 '15 at 15:45
  • given this line(in the question) " have 2 sets of C++ files, one set which I only have the .o files from GCC but no source code, while the other set in Microsoft Visual Studio 2015 which I have the full source code -" 1) you do not have '2 sets of C++ files' 2) you have a set of C object files 3) a set of C++ source files. Of particular importance is that C object files cannot (easily) use C++ library files (however, if the libraries are compiled with appropriate use of the '#ifdef cplusplus extern "C" ... #endif' sequence they can be made to talk to each other – user3629249 Oct 15 '15 at 15:58

1 Answers1

2

I am afraid you are out of luck. You see every compiler mangles method names differently in c++. In the object code a method in a class has to be encoded with parameters, return type and membership. The best approach I think would be to build a DLL via GCC then use that in VS. Clang on Windows is still a headache.

Amer Agovic
  • 380
  • 2
  • 6
  • I've already tested the DLL approach as a fallback plan, but wanted to explore a bit more before I give up on 1 single EXE file. – Rascal the Raccoon Oct 15 '15 at 16:14
  • 2
    Hmm, how about writing a wrapper in C and exposing your C++ code via C methods. You would end up with your foreign .o files and one more that is pure C. I think C method names are mangled the same way in all compilers.... how you mix OBJ and O files there you might have to look for a tool if it can't be done naively. – Amer Agovic Oct 15 '15 at 16:54
  • Thanks. I edited my initial question as I only found out later that such a tool exists. It seems uncommon though. I'll test it out and maybe look into it a bit more, but I'm not confident with using a non-mainstream tool to mess around with OBJ and O files. Changing some source code syntax here and there is still something I might be able to understand and feel OK with, but OBJ and O file formats are way beyond my level. However, GCC is rejecting a lot of some pretty advanced looking C++ template stuffs in the MS VS source files, so without help, I don't think I can make them compile in GCC. – Rascal the Raccoon Oct 16 '15 at 04:28