Error on Linking a C library: libsodium.la file not recognized - c

I want to use a library in C so I've downloaded it (libsodium) and I'm trying to use it with a simple program and I cannot make Codeblocks to recognise it.
I get the following error
libsodium.la: file not recognized: file format not recognized
collect2.exe: error: ld returned 1 exit status
My Specs:
Architecture x64
Codeblocks
Windows 10
MinGW
I'm using the pre-builts available in the library resources and copying them into MinGW local folder and binding them to the linker following a codeblocks tutorial
What am I possibly doing wrong?

Was the prebuilt library also x64?
Did you link with the .la file instead of the .a file by mistake?
In Code::Blocks add the path where libsodium.a is to the library search path and add sodium as a library (or -lsodium as linker flag)
Have you tried building libsodium yourself? If you have MSYS2 it's not that hard:
# change the following line to the location where you want to install libsodium
INSTALLPREFIX=/usr/local
./configure --prefix=$INSTALLPREFIX --enable-blocking-random LDFLAGS="-Wl,--as-needed -lssp" &&
make install-strip &&
echo Success

Related

Why gcc under Windows O.S. produces a .o instead of a .lib file when compiling static libraries?

I am using gcc 8.1.0 on Windows. To install it I set up Code::Blocks on my computer and updated the environment variable list by adding the path to the gcc.exe program within the installation folder of CodeBlocks. The file editor I used was the built-in editor in Visual Studio. The terminal to compile was the power shell from Visual Studio as well.
In the library development folder I have the files mul.c and mul.h. Their content is irrelevant.
To compile the library I use the command:
gcc -c mul.c
When I run it, it creates a file object mul.o and not mul.lib. I needed to use the option -o mul.lib to successfully create the desired extension file. After placing the header, the .lib file and the main.c in the same parent folder I am obvioudly able to build the executable by running.
gcc main.c -I./include -L/static -lmul -o my_program.exe
I have two questions:
Why does gcc produces a .o if I am in a Windows environment?
I followed a tutorial that compile the static library under Linux and it names it libmul.o, in this way the -lmul option is able to retrieve the library. But if I call my generated static library libul.lib it generates the error:
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-ingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmul
collect2.exe: error: ld returned 1 exit status
Are these a normal behaviours by gcc, or is it side effect of making gcc available just by updating the Windows environmental variables list?
Thank you to the community in advance.
GCC comes from the *nix world where libraries have the .a extension. When using GCC+MinGW this remains the case.
Shared libraries in MinGW are .dll files but their libraries for linking are .dll.a files.
The advantage of .a files is that a lot of sources build out of the box on Windows with MinGW, especially when using MSYS2 shell.
If you use -l it will look for .a (or .dll.a for shared build) file adding the lib prefix and the extension automatically.
So -lmul will look for libmul.a (static, e.g. when --static linker flag is given) or libmul.dll.a (shared).
By the way, you are using quite an old GCC 8.1.0.
As I write this current version is 12.2.0. Check https://winlibs.com/ for a standalone download (instructions on how to configure in Code::Blocks are on the site) or use MSYS2's package manager pacman.

Linking libraries with a MinGW project in VS code

I am using MinGW version 4.5.2 on windows 10. I am trying to link multiple libraries to a project in VS code. When I try run this command: g++ main.c -o main.exe -I"C:\\Users\\USER\\Programming\\OPenGL Library\\glfw\\include\\glfw3.h" -L"C:\\Users\\USER\\Programming\\OpenGL Library\\glfw\\lib-mingw-w64\\". I get a compile error: main.c:2:24: fatal error: GLFW\glfw3.h: No such file or directory What is wrong with the command above that gives this linking error. As far as I can tell I have pointed MinGW to the library and specified the header file. I am not sure what else I need to add to the command for link the GLFW library as well as other libraries such as GLEW or glm.
If your include directive looks like
#include <GLFW/glfw3.h>
you need to supply a path to a directory which itself contains a directory named GLFW.

How to compile a C program using a library

I'm fairly new to programming in C. For my thesis I have to use the library "Libquantum" from the site http://www.libquantum.de/. I write my code in notepad and compile using the command window. I downloaded mingw and compile with gcc.
For normal C programs everything works fine. I use, for example, the command gcc -o HelloWorld HelloWorld.c and the program compiles fine. But I can't manage to compile programs where I use the library...
I downloaded the library and used the commands ./configure, make, ..., make install like they mentioned in the installation guide (https://github.com/jonbaer/libquantum/blob/master/INSTALL). After the last command the command window showed this:
I made sure to include #include <quantum.h> in the code and tried the linker flag -lquantum on different places when I try to compile but nothing works...
When I try to compile i get the following error
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lquantum
collect2.exe: error: ld returned 1 exit status
As the make install output says you need to tell the linker where the library is using a -L<path> option. The make install places the library in \usr\local\lib. If you are performing the build in MSYS, you can probably use -L\usr\local\lib, but if you have moved the file, change as necessary.
If you are building from cmd.exe (Windows's shell) rather than MSYS, you'll have to use its native a Windows path.

.dll files required to run .exe file on other computers (c, gtk3)

I have an application that is written in c, compiled with gcc (mingw), on windows 7 64bit. and it uses gtk3 libraries.
I intend to make it a standalone file, portable application, that wouldn't require anything to be pre-installed, nor installed.
if I try to run it on other machines, it fails to launch "some-gtk-library.dll is missing"
I suspect that putting all the missing .dll files in the same folder would make the program run, but I want to know how can I integrate all the needed files in the .exe file
EDIT 1
here is the error I get if I try adding the option -static in the linking stage of gcc
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgtk-3
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgdk-3
C:/msys64/mingw64/lib\libgobject-2.0.a(libgobject_2_0_la-gtype.o):(.text+0x99b0): multiple definition of `DllMain'
C:/msys64/mingw64/lib\libgdk_pixbuf-2.0.a(gdk-pixbuf-io.o):(.text+0x1f10): first defined here
collect2.exe: error: ld returned 1 exit status

gcc library linking issue in Windows after cygwin installation

C:\slite\1.1>gcc -c ".\src\SDR.c" -o ".\obj\SDR.o" -I".\inc"
C:\slite\1.1>gcc ".\obj\SDR.o" -o ".\exe\SDR.exe" -L".\lib" -lsres -lshis
.\lib/sres.dll: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Could not compile C program using gcc after installing cygwin in Windows 2003.
Before installing cygwin, I was able to compile successfully.
I am compiling the code from Command prompt, not from cygwin terminal.
The gcc being used is from Dev-Cpp distribution.
Is the problem because of the directory separator ('/') between the libraries directory specified by -L and individual libs specified by -l, being used by gcc after cygwin was installed?
If i try to compile in cygwin terminal I am getting unknown function calls error, though the functions are defined in the headers included.
EDIT:
C:\srmlite\x7.5.0.146-1600>where gcc
C:\Dev-Cpp\bin\gcc.exe
The gcc was not from cygwin.
I have uninstalled cygwin and removed its directory too. I still see this problem.
Try to add the correct bin directory into the path in your Environment variables.
Something like this: C:\soft\cygwin\bin
Seems that Cygwin modified the default environment, adding itself to %PATH%, so you're actually calling cygwin's gcc, check that out.

Resources