Call unmanaged g++ .lib in C++/CLI - visual-studio-2010

is it possible to wrap a static library which was compiled using g++ in C++/CLI?
Here is my try:
Unmanaged C++
#include <string>
class Person
{
public:
Person(void);
~Person(void);
void Print(std::string, std::string);
};
C++/CLI
#pragma once
#include <Person.h>
#pragma comment (lib, "VS-Person.lib")
//#pragma comment (lib, "GPPPerson.lib")
namespace CppWrapper {
public ref class PersonWrapper {
private:
Person* m_person;
protected:
public:
PersonWrapper(); // Doing 'm_person = new Person();'
~PersonWrapper();
};
}
The think is, when using the lib compiled in a VS2010 solution (VS-Person.lib), it works perfectly but when using the lib compiled with g++ (GPPPerson.lib), I got the following errors:
error LNK2028: jeton non résolu (0A00032D) "public: __thiscall Person::Person(void)" (??0Person##$$FQAE#XZ) référencé dans la fonction "public: __clrcall CppWrapper::PersonWrapper::PersonWrapper(void)" (??0PersonWrapper#CppWrapper##$$FQ$AAM#XZ)
error LNK2019: symbole externe non résolu "public: __thiscall Person::Person(void)" (??0Person##$$FQAE#XZ) référencé dans la fonction "public: __clrcall CppWrapper::PersonWrapper::PersonWrapper(void)" (??0PersonWrapper#CppWrapper##$$FQ$AAM#XZ)
Did I miss something?
How can I wrap an unmanaged library compiled with g++ in C++/CLI?
Thanks,

It's not possible. G++ and Visual C++ have wildly different expectations about object/library files. It's possible to recompile the unmanaged portion in Visual C++ and link to that.
Alternatively, you can build a DLL in G++ and P/Invoke that from C++/CLI.
EDIT: since it's a C++ library (as opposed to C), doubly impossible. G++ and Visual C++ won't agree on name mangling; so none of the C++ symbols would match upon linking/dynamic loading.
That said, why are you doing this in the first place? Mixing coding paradigms (managed and unmanaged, in your case) within one module is a bad idea in general. Is it really about the Person class? While I won't deny that unmanaged C++ has its charms, mixed mode projects have no charm at all.

Related

Using minGW to create a static library for use with Intel Fortran

Let me start off by stating that I am not a programmer. I am a Mech Eng that has to use code from various other engineers and assemble them into an executable. The main code is fortran and is compiled using Intel Fortran via Visual Studio 2008 (yes, very old version). One of the supporting codes is c++. Normally, I compile the c++ code using the built in Visual c++ compiler into a static lib then link the static lib into the main fortran code. The latest update of the c++ code will not compile in the 2008 Visual c++ because it needs the /permissive option. So, I have tried to get the c++ code compile into a static lib using g++. I get unresolved external symbol errors when doing this. The c++ code I use it very complex so I decided to make a simple example to see if I could get that to work. I am getting similar errors but since it is so simple I can post it here.I am sure someone on this forum can fix it in a few min. Here is the example c++ code:
#include <fstream>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
//------------------------------------------------------------------------
extern "C"
{
void add_2_nums(double *x,double *y,double *z);
}
extern "C" void add_2_nums(double *x,double *y,double *z)
{
*z = *x + *y;
return;
}
I compiled it using both g++/ar and mingw32-g++/mingw32-gcc-ar. Here is an example command:
mingw32-g++ -s -Iincludem -Iincludev -c -O2 -DNDEBUG add_2_nums.cpp
mingw32-gcc-ar r add_2_nums.lib add_2_nums.o
Some of the compiler options I used was from this forum in trying to resolve the errors and may not be appropriate.
Here is the simple fortran code:
program gcc_fun
implicit none
! Variables
real*8 x,y,z
x = 3.d0
y = 17.d0
call add_2_nums(x,y,z)
! Body of gcc_fun
print *, 'Z = ', z
end program gcc_fun
I use the C,Reference compile option and the rest default inside the Visual Studio environment. Here are the errors I get:
Error 1 error LNK2019: unresolved external symbol __ZNSt8ios_base4InitD1Ev referenced in function ___tcf_0 add_2_num.lib(static_lib_test.o)
Error 2 error LNK2019: unresolved external symbol __ZNSt8ios_base4InitC1Ev referenced in function __GLOBAL__sub_I_add_2_nums add_2_num.lib(static_lib_test.o)
Error 3 fatal error LNK1120: 2 unresolved externals Debug\gcc_fun.exe

QuantLib Date class in Visual C++ 2010

I just started this simple Quantlib date class in VC++ Express 2010:
#include <iostream>
#include <sstream>
#include "ql/time/date.hpp"
int main(int, char* [])
{
QuantLib::Date d(1, QuantLib::January, 2010);
std::cout<<da<<std::endl;
}
When I compiled it, this is one of the errors:
1>ql_inout.obj : error LNK2019: unresolved external symbol "public: __thiscall QuantLib::Date::Date(int,enum QuantLib::Month,int)" (??0Date#QuantLib##QAE#HW4Month#1#H#Z) referenced in function _main
It must be something I didn't setup correctly in 2010 project. I have compiled the library in Debug mode successfully.
Not all headers include the pragma that tells the linker to add QuantLib. If you don't want to include the full headers—which is advisable, as they would increase a lot your compilation time—you can add
#include <ql/auto_link.hpp>
to the included headers.
(You could also add the library explicitly to the linker options, but that is a lot more work since you have to specify different library names depending on the configuration. auto_link.hpp does this for you.)

error LNK2019: unresolved external symbol - only .h files

Argh... I've been struggling lately to make Visual Studio 2010 (VC++) include a bunch of 3rd party libraries I wanna use in my project. That's the issue: The linker seems not to be able to determine every symbol that is generated in my code which come from the 3rd party libraries definitions. I've included the header files path on my include directories and also the sources path on my source directory, but it is still not working. I've googled it for a while and in most of cases, the issues is cause by a missing reference of the .lib file on linker's additional dependencies, however the library don't come with them.
Here's a piece of sample code:
#include "stdafx.h"
#include <fuzzylite\FuzzyEngine.h>
int _tmain(int argc, _TCHAR* argv[])
{
fl::FuzzyEngine eng;
return 0;
}
So that's the output VS shows
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall fl::FuzzyEngine::~FuzzyEngine(void)" (??1FuzzyEngine#fl##UAE#XZ) referenced in function _wmain
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: __thiscall fl::FuzzyEngine::FuzzyEngine(void)" (??0FuzzyEngine#fl##QAE#XZ) referenced in function _wmain
So i wonder if is there a way to build my sources with the .h and .cpp files of my 3rd party library.
Thank you.
Caio
Check the new version of fuzzylite-2.0 at http://www.fuzzylite.com. That problem has been solved.
Windows requires to add __declspec(dllexport) to classes, which was not present in previous versions. Today, every class starts with class FL_EXPORT Engine, where FL_EXPORT is the missing definition.

Visual studio - prevent linking of static library

I am creating a static library in Visual Studio 2010. This library will be statically linked with another of my applications to produce the .exe. The thing is that I want my .exe to be statically linked against C adn C++ library (don't want dependency on msvcp100.dll and msvcr100.dll). But what ever I do I can't get it working.
If I link my static library with static C and C++ libs then I can't compile the .exe - Linker complains about "already defined symbols".
If I link my static library with C and C++ DLL then my .exe ends up having dependency on msvcp100.dll and msvcr100.dll.
How do I tell VS to link my static library with static C and C++ libs only when it's being linked in my .exe?
EDIT
Here are a fre linker errors when both static lib and .exe user /MT (i.e. static linking of runtime library):
1>msvcrt.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info##AAE#ABV0##Z) already defined in LIBCMT.lib(typinfo.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref#facet#locale#std##QAEPAV123#XZ) already defined in re2_release.lib(regexp.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "protected: virtual class std::basic_streambuf<char,struct std::char_traits<char> > * __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::setbuf(char *,__int64)" (?setbuf#?$basic_streambuf#DU?$char_traits#D#std###std##MAEPAV12#PAD_J#Z) already defined in re2_release.lib(regexp.obj)
1>msvcrt.lib(MSVCR100.dll) : error LNK2005: _strtoul already defined in LIBCMT.lib(strtol.obj)
1>LIBCMT.lib(crt0init.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
If I set /NODEFAULTLIB:LIBCMT then errors from mscvrt.lib disappear, but msvcprt.lib errors remain.
I had this problem, and libriks solution solved it. I will quote him here for those who skips reading the comments:
Some module is not using static linking of the runtime library, because your linker errors >show "msvcrt.lib" which is the import library for the DLL version. Look more carefully >through your project settings, and any other libraries you link to, making sure EVERYTHING is >using /MT.

linking with boost unresolved extern link error

Visual Studio 2005
I am linking with boost libraries release 1_33_1.
I keep getting this link error.
libboost_thread-vc80-mt-sgd-1_33_1.lib(once.obj) :error LNK2001: unresolved external symbol "public: void __thiscall std::_String_base::_Xran(void)const " (?_Xran#_String_base#std##QBEXXZ)
Does anyone have any suggestions?
Many thanks,
Edit ======
After recompiling the boost libraries using these switches:
C:\boost_1_42_0>bjam --build-dir=d:\boost_1_42 --build-type-complete --toolset=msvc-8.0 address-model=32 architecture=x86
I am getting some of the following errors:
1>msvcrtd.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info##AAEAAV0#ABV0##Z) already defined in LIBCMTD.lib(typinfo.obj)
1>msvcrtd.lib(MSVCR80D.dll) : error LNK2005: __wassert already defined in LIBCMTD.lib(wassert.obj)
Anyone have any ideas?
Many thanks,
First of all you don't have to set address-model and architecture explicitly if building 32 bit. Also your call has an error: --build-type-complete should be --build-type=complete. If I'm not wrong your call builds absolutely all libs, including filesystem, regex etc.
Obviously you need boost::thread only, so I would recommend to try this call:
C:\boost_1_42_0>bjam --build-dir=d:\boost_1_42 --build-type=complete --toolset=msvc-8.0 --with-thread
But I don't think that will solve your prob but you could give it a try.
My guess is that you somehow mixed /MD and /MT in the project settings.
See these links: First, second
What is probably happening is that your project is linking to the static debug version of the C++ runtime, which causes the static debug boost thread library to be linked and is also linking to another static library which was dynamically linked to the C++ runtime.
The sgd tag in libboost_thread-vc80-mt-sgd-1_33_1.lib means that the boost thread library you're linking with was built against the static debug version of the C++ runtime.

Resources