Home > Mobile >  Integrating .lib files with C code written on Linux
Integrating .lib files with C code written on Linux

Time:01-14

I have pre-existing code that was written and is running on a Linux machine. I need to add to this code, this new code which depends on a library using VS. I have decided to develop on Visual Studio using WSL (Windows Subsystem for Linux) then taking the executable/out file to my Linux machine and running it there.

The code I am adding myProg.c uses a function Func(double arg) which comes from the library Reallib.lib

I have included the header file

#include "Support_Files/Reallib.h"

In my project property pages, the .lib file is in the Additional Dependencies

Support_Files/Reallib.lib

The declaration in the Reallib.h file:

long Func(double arg)

At build-time, these are the errors I get:

/mnt/c/Users/mitch/Projects/myproject/myproject/myproject/obj/x64/Debug/myProg.c: in function `main':
undefined reference to Func
ld returned 1 exit status

If I'm including my library already, why am I getting this error? Is it because Linux systems don't use .lib files? How can I combine the code written on Windows with code written on Linux?

Researching online doesn't seem to match my similar problem.

I have already tried using GCC for Remote Linux, but a similar error was produced. But neither (WSL or Remote Linux) seem to play nice with combining .lib files with source code written on Linux.

Now, I could add RealFunc.c and that would clear the error up, but I would just get another undefined reference error, and so on and so forth (RealFunc.c has plenty of other dependencies that are taken care of in Reallib.lib)

Configuration properties > General > Platform Toolset

I've also tried developing only on Linux, bringing the .lib file over and updating my makefile to link that, but it wasn't working (I believe because Linux doesn't use .lib)

What should be my route of action? I NEED the code that is in Reallib.lib. Do I need to bite the bullet and essentially recompile all the source code in Reallib.lib? Do I need to just move over to Linux 100% and use VS Code? I like using VS because it makes the compiling and linking options much easier. I am still new to C and Linux.

I am using Visual Studio 2022 17.4.3

WSL Ubuntu (v20.04)

$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

CodePudding user response:

There is no proper/native way to use a Windows library .lib in Linux, your only solution is by re-compiling the library for the targeted platform, in this case, is Linux.

Option A

Simply re-compile the library source code in Linux using GCC or Clang to generate the proper .a static library or .so shared library.

Option B

Install a cross compiler in Windows for Linux, you can use Cygwin if you are familiar with it.

  • Related