Linking a .lib file on Windows 7 - c++

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.

Related

Use MinGW static library (.a) within Visual studio environment

I work on a project that generate several static libraries (.a) from MinGW with Scons project files.
And I need to use these libs into an other project that work on C++ Visual Studio 2013.
First I tried to just add MinGW lib to my VS project and obviously it didn't work out.
I read some things about using a ".def" file to switch from ".lib" to ".a" but found nothing about convertion from ".a" to ".lib". But I have no idea how to get this ".def" file.
Does anyone have some clue that what I am trying to realise is possible (use MinGW into VS2013) ? Or even a solution that can help me :)
Mathieu.

gcc compiled library on VS using ext/hash_map

I'd like to use a library I've compiled using Cygwin GCC (.a) in a Visual Studio C++ project. When I include headers from the library in VS, the ext/hash_map header is missing. Is that a header file I can just add and replace with hash_map, or is it all together hopeless (because the library doesn't make heavy use of hash maps)? Moreover, is it ridiculous to hope VS can use the .a library? Thanks!
You cannot mix GCC generated .a files with Visual Studio.

DLL include unordered_map is not compiling with visual studio compiler

I am trying to compile a DLL with MinGW and use it from an executable compiled with visual studio compiler.
One of source files from DLL is using hash_map<> and it can be compile with MinGW successfully.
When I change hash_map<> to std::tr1::unordered_map<> and add #include <tr1/unordered_map> to my code it's compiling successfully for visual studio compiler.(How can I force MinGW to use tr1 namespace?)
But When I am trying to compile the code with MinGW as a DLL and use it from an executable compiled with visual studio compiler it's giving error: cannot open include file 'tr/unordered_map'
must My DLL be compatible with cl and MinGW same time?
EDIT:
my commands for compiling are below:
g++ -shared -o stemming.dll stemming.cpp alphabet.cpp basic.cpp determinise.cpp fst.cpp hopcroft.cpp operators.cpp utf8.cpp -Wl,--output-def,stemming.def,--out-implib,libstemming.a
lib /machine:i386 /def:stemming.def
cl sfstMinGW.cpp SFST/stemming.lib
VC++ is trying to open a header file and can't find it in the include path. VC uses the INCLUDE environment variable to determine the paths to use when searching for header files. Since VC does not use the tr/ directory it's not going to find it. You need provide include statements for both VC and g++ and choose which one to use like below.
#if defined(_MSC_VER)
# include <unordered_map>
#else
# include <tr/unordered_map>
#endif
You need to make sure that you compile the application using the same implementation of unordered_map used by the DLL. This means you will need to update the include paths to use GCC's version of TR1 instead of MS's implementation of the standard headers.

Linking OpenCV static libraries in Eclipse Windows

I want to create an executable that I can move to another computer that does not have OpenCV installed.
As such I am trying to statically link all the libraries needed into the executable (Thats what its called right?).
The program compiles just fine and works on my local computer but when I copy it to another computer it complains that it is missing .dll files and won`t execute.
I am using eclipse juno with mingw as compiler on windows 7.
My progress so far:
I have included the libraries needed from opencv\build\x86\mingw\lib into the MinGW C++ linker -> libraries in the project properties.
opencv_core244.dll
opencv_highgui244.dll
opencv_imgproc244.dll
In the original folder these are called:
libopencv_core244.dll.a
libopencv_highgui244.dll.a
libopencv_imgproc244.dll.a
I have set the linker flag in MinGW C++ Linker -> miscellaneous to -static.
I have been searching a lot for answers and have tried a few different things but I`m really stumped by this.
How do you force the compiler in eclipse (MinGW in this case) to link the libraries as static libraries and not as dynamic libraries as it is apparently doing?
1) If OpenCV has been compiled to be used as DLLs, then you can not link statically.
2) Remember when you compile the project for DLL output then you have a .lib file. This .lib file is not actually a static library with code. It is used by the compiler to know the DLL name for class/function definitions. This will need the DLL at runtime.
3) For static linking you need .lib file which compiled as static library. In such compilation there is only one output which is a .lib file. This .lib file can be used for static linking and the code from .lib file is added to your application.
4) I have just compiled one project with VStudio which is a DLL having only one function. I get a DLL and a .lib as output. The size of the .lib is 2kb.
5) When I compile the same project as static library, then I get only one output that is .lib file. Its size is 133kb.
This is almost a year late with the clarification/answer, but hope it can be some use to you or anyone else who comes across this page with the same problem.
While Pruthviraj didn't clearly explain his answer, he had the right idea. By default, the config file for cmake has "BUILD_SHARED_LIBRARY" flag set to true. The .a files that were created simply redirected to the dll files and are in essence useless to statically link in a program.
The easiest way is to rebuild OpenCV with cmake, but make sure the flag "BUILD_SHARED_LIBRARY" is set to false. It is under the Build sub-category in cmake GUI if you are using that.
The new generated make files should produce static libraries only in the lib folder and should properly link opencv statically in your program. Hope it helps!

Generating dll file from object files

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.