用g ++链接到外部库 [英] link to external library with g++

查看:171
本文介绍了用g ++链接到外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了 FMUSDK ,我想编译代码.不幸的是,该构建脚本应该可以与Visual Studio C编译器一起使用,但是我不能在我的机器上安装/使用它.因此,我尝试修改脚本(以及代码的某些部分)以使其能够与GCC一起编译.但是我对编译复杂的代码是全新的(我通常只使用gcc * .c -o outfile.exe)

I downloaded the FMUSDK and I want to compile the code. Unfortunately the build script is supposed to work with the Visual Studio C compiler, but I can't install/use it on my machine. Thus, I try to modify the script (and some parts of the code) to be able to compile it with the GCC. But I am completely new to compiling complex code (I usually just use gcc *.c -o outfile.exe)

这是我的问题: 在某些文件中,有一个与

Here is my problem: In some files there is a library linked with

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

这不适用于GCC.

可以在..\shared\parser\libxml2.lib中找到该库 头文件位于..\shared\parser\libxml\

The lib can be found in ..\shared\parser\libxml2.lib with headers files in ..\shared\parser\libxml\

我用以下命令调用构建脚本

I call the build script with

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

脚本如下:

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

我尝试使用在 http://www .rapidtables.com/code/linux/gcc/gcc-l.htm ,但我似乎无法正常工作.

I tried to use what I found on http://www.rapidtables.com/code/linux/gcc/gcc-l.htm, but I seems not to work.

有人可以帮忙吗?我究竟做错了什么?在INC和LIB之下,我把所有看起来有用的东西都放了进去,但是我不知道我在那儿实际上在做什么...

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 ...

我还有一个日志文件,其中包含批处理文件的详细输出.但我看不到如何在这里上传它:(

I have also a logfile with the verbose output of the batch file. But I don't see how to upload it here :(

最诚挚的问候, 马丁

推荐答案

libxml2.lib是(很多时候)搞混库名称约定的示例. Visual Studio使用添加.lib扩展名(这将是xml2.lib)的约定,GCC使用添加lib前缀和.a扩展名到库名的约定(这将是libxml2.a).

libxml2.lib is the example of (quite frequent) messing with library name conventions. 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).

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

According to GCC manual (https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Link-Options.html#Link-Options) you have two options:

  1. 将库文件名更改为libxml2.a,因为这是GCC在提供-lxml2选项时正在寻找的文件名.
  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.

链接器在标准目录列表中搜索该库,该库实际上是一个名为lib library .a的文件.

  • 提供库文件的全名,而不使用-l(例如,在g ++命令行中将-lxml2更改为libxml2.lib),那么GCC会精确查找给定名称.

  • Provide the full name of the library file , without -l (e.g. change -lxml2 to libxml2.lib in g++ command line), then the GCC will look for the given name exactly.

    未以特殊的公认后缀结尾的文件名被视为命名目标文件或库. (链接器根据文件内容将目标文件与库区分开.)如果完成链接,则这些目标文件将用作链接器的输入.

    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.

  • 这篇关于用g ++链接到外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆