简体   繁体   English

用g ++链接到外部库

[英]link to external library with g++

I downloaded the FMUSDK and I want to compile the code. 我下载了FMUSDK ,我想编译代码。 Unfortunately the build script is supposed to work with the Visual Studio C compiler, but I can't install/use it on my machine. 不幸的是,该构建脚本应该可以与Visual Studio C编译器一起使用,但是我不能在我的机器上安装/使用它。 Thus, I try to modify the script (and some parts of the code) to be able to compile it with the GCC. 因此,我尝试修改脚本(以及代码的某些部分)以使其能够与GCC一起编译。 But I am completely new to compiling complex code (I usually just use gcc *.c -o outfile.exe) 但是我对编译复杂的代码是全新的(我通常只使用gcc * .c -o outfile.exe)

Here is my problem: In some files there is a library linked with 这是我的问题:在某些文件中,有一个与

 #pragma comment(lib, "libxml2.lib")

This does not work with GCC. 这不适用于GCC。

The lib can be found in ..\\shared\\parser\\libxml2.lib with headers files in ..\\shared\\parser\\libxml\\ 该库可以在..\\shared\\parser\\libxml2.lib ,其头文件位于..\\shared\\parser\\libxml\\

I call the build script with 我用以下命令调用构建脚本

fmusdk\fmu20\src\co_simulation>..\build_fmusim_cs_gcc.bat

The script then looks like this: 脚本如下所示:

set SRC=main.c ..\shared\sim_support.c ..\shared\xmlVersionParser.c ..\shared\parser\XmlParser.cpp ..\shared\parser\XmlElement.cpp ..\shared\parser\XmlParserCApi.cpp
set INC=-I ..\shared\include -I ..\shared -I ..\shared\parser\libxml -I ..\shared\parser 
set LIB=-L ..\shared\include -L ..\shared -L ..\shared\parser\libxml -L ..\shared\parser 
set OPTIONS=-D FMI_COSIMULATION -D STANDALONE_XML_PARSER -D LIBXML_STATIC -v

g++ %INC% %OPTIONS% %SRC% %LIB% -lxml2 -o fmu20sim_cs.exe 

But I get the following error message: 但是我收到以下错误消息:

c:/users/<username>/userprograms/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lxml2

I tried to use what I found on http://www.rapidtables.com/code/linux/gcc/gcc-l.htm , but I seems not to work. 我尝试使用在http://www.rapidtables.com/code/linux/gcc/gcc-l.htm找到的内容 ,但似乎无法使用。

Can someone help? 有人可以帮忙吗? What am I doing wrong? 我究竟做错了什么? Under INC and LIB I put everything that looked helpful, but I have no idea what I am really doing there ... 在INC和LIB之下,我把所有看起来有用的东西都放了进去,但是我不知道自己在做什么。

I have also a logfile with the verbose output of the batch file. 我还有一个日志文件,其中包含批处理文件的详细输出。 But I don't see how to upload it here :( 但我看不到如何在这里上传:(

Best regards, Martin 最好的问候,马丁

You should replace -lxml2 by -llibxml2 . 您应该将-lxml2替换为-llibxml2

Explaination, from Mingw : 来自Mingw的解释

Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; 还要注意,库名称"lib<name>.a""lib<name>.lib"是不相同的; if you have a library named according to the aberrant "lib<name>.lib" convention, it will not be found by an "-l<name>" specification -- if you cannot rename the library, you must use the form "-llib<name>" instead. 如果您有一个根据异常的"lib<name>.lib"约定命名的库,则"-l<name>"规范将找不到该库-如果无法重命名该库,则必须使用"-llib<name>"代替。

libxml2.lib is the example of (quite frequent) messing with library name conventions. libxml2.lib是(经常)弄乱库名称约定的示例。 Visual Studio uses the convention of adding .lib extension (this would be xml2.lib ), GCC uses the convention of adding lib prefix and .a extension to library name (this would be libxml2.a ). Visual Studio使用添加.lib扩展名的约定(这将是xml2.lib ),GCC使用添加lib前缀和.a扩展名的约定到库名(这将是libxml2.a )。

According to GCC manual ( https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Link-Options.html#Link-Options ) you have two options: 根据GCC手册( https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Link-Options.html#Link-Options ),您有两个选择:

  1. Change the library file name to the libxml2.a , because that is the file name GCC is looking for when given the -lxml2 option. 将库文件名更改为libxml2.a ,因为给定-lxml2选项时,这是GCC正在寻找的文件名。

    The linker searches a standard list of directories for the library, which is actually a file named lib library .a. 链接器在标准目录列表中搜索该库,该目录实际上是一个名为lib library .a的文件。

  2. Provide the full name of the library file , without -l (eg change -lxml2 to libxml2.lib in g++ command line), then the GCC will look for the given name exactly. 提供库文件的全名,不带-l (例如,在g ++命令行-lxml2更改为libxml2.lib ),那么GCC将精确查找给定名称。

    A file name that does not end in a special recognized suffix is considered to name an object file or library. 没有以特殊的识别后缀结尾的文件名被视为命名目标文件或库。 (Object files are distinguished from libraries by the linker according to the file contents.) If linking is done, these object files are used as input to the linker. (链接器根据文件内容将目标文件与库区分开。)如果完成链接,则这些目标文件将用作链接器的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM