开发者

How to add 3rd party libraries to MinGW?

I've just set up Mi开发者_如何学运维nGW environment following this post

But how to add 3rd party libraries to it?


A library consists of two main components - the C header files and the compiled object code archive. GCC has a bewildering array of ways of specifying these things, but let's say you are using a library foo.a which lives in the relative directory path foo/lib, and a header foo.h which lives in foo/inc. Your own C code lives in main.c and looks like this:

#include "foo.h"
int main() {
  return FooFunc();    // call function in foo.a
}

To compile this, you could use the command line:

gcc main.c -Ifoo/inc foo/lib/foo.a -o main.exe

the -I flag adds to the path searched for headers. You can also add to the lib path, but then things start to get complicated :-)


No different from any other system using gcc

  • get the sources
  • untar
  • run configure -- this may require a tweak or two
  • make
  • make install

and now use your new library with proper -Lfoo/bar -lfoobar switches.

I recommend the MSys system around MinGW in order to do all this.


I just went through figuring this out myself. I strongly recommend reading the linking howtos on the MinGW webpage. Read them carefully as there is quite a lot to take in. They are quite thorough though so it's well worth your time.

There are essentially two ways of doing things. First, you can just regard MinGW as a compiler, because that's what it is, and invoke it from cmd.exe (the command prompt) or an IDE. The other way to do it is to use MSYS which is basically a Unix style shell that you can run on Windows to use Unix style build tools like configure and make.

For either of these you really have to read the howto I linked above to understand how dependency paths are being searched. I hesitate to explain it here as it is already explained in the howtos and duplicating that information is not a good idea. If you have specific questions after reading them I'd be happy to offer more help.


As this has taken me several hours of research on SO to get this 3rd party run under MinGW, a clear example might help other people who are as new to the topic as me. The question was how to add the 3rd party libraries, and the answer is: you better do not really add them, you just use them as arguments (as already stated by @anon). I refer to @kjoppy's link recommendation. From http://mingw.org/wiki/IncludePathHOWTO, at the bottom:

As a variation on option (2), create a separate directory hierarchy, for each and every individual external library package to be locally installed; this mitigates all of the disadvantages of option (1), and the package intermixing of option (2), but it incurs an alternative penalty:-- As with option (2), appropriate "-I DIR" and "-L DIR" options must be added to each GCC invocation; however, whereas option (2) requires only one additional path of each type, this option requires an additional one of each type, for each and every external library to be deployed. (While this may still be achieved by appropriate customisation of the GCC Specs File, or by appropriately specifying the CPATH and related environment variables, the additional complexity, and maintenance overhead, may be considered unacceptable to many users).

Summing up, they say that the options of GCC Specs File or CPATH as "real 3rd party lib integration" are too much of additional maintenance overhead for the "normal" user, and that we should rather invoke like in the following example:

hello.c:

#include <stdio.h>
#include <mysql.h>
int main()
{
    printf("Hello world\n");
    return 0;
}

I include mysql.h using a mariadb connector, where the include and the lib is saved separately from gcc defaults. This can be invoked from the commandline (I is include, L is library, the space after these letters is optional)

gcc -o hello hello.c -I "C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\include" -L "C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\lib\\" -lmariadbclient

If you instead want to run a build task in VS Code, I recommend using the extension C/C++ Compile Run (extension id: danielpinto8zz6.c-cpp-compile-run) by pressing F7 and then entering only the arguments:

-I "C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\include" -L "C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\lib\\" -lmariadbclient

while the c_cpp_properties.json must look like:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

This is repeating some answers from here or other similar questions, but they have not helped me enough to get this really started quickly, that is why I post a new answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜