HarryKane
HarryKane

Reputation: 168

link to external library with g++

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")

This does not work with GCC.

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

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.

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 :(

Best regards, Martin

Upvotes: 3

Views: 9451

Answers (2)

boorg
boorg

Reputation: 146

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

According to GCC manual (https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Link-Options.html#Link-Options) you have two 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.

    The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a.

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

Upvotes: 4

Mathieu
Mathieu

Reputation: 9619

You should replace -lxml2 by -llibxml2.

Explaination, from Mingw :

Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; 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.

Upvotes: 3

Related Questions