Linking static C++ archive file in Hello World app - c++

I'm trying to link an archive file into my simple hello world app, to make sure I understand the process. However, I obviously don't, because the library isn't linking correctly.
Here is my simple app (hello.cc):
#include <iostream>
#include "firebase/app.h"
int main()
{
std::cout << "Hello, world!\n";
std::cout << std::endl;
return 0;
}
And here is my compilation command with linking:
gcc hello.cc -L /tmp -l app -o hello -lstdc++
I've moved my archive file (libapp.a) to /tmp.
My understanding is that -L <dir> adds directories to search for lib*.a files and -l <name> indicates the name of the archive files, in the form of lib<name>.a. I know it isn't linking the archive file, because I get this error:
hello.cc:3:26: fatal error: firebase/app.h: No such file or directory
#include "firebase/app.h"
However, I also don't think the compilation command does what I think it does, because I renamed libapp.a to libapp.a2 and the same error was returned, not something indicating that the archive file was missing.
Can somebody help me with a) the command to link the library file /tmp/libapp.a and b) explain what I'm doing wrong?
EDIT
I forgot to include the headers, which caused the initial error I think. Now that I'm including the headers, with the following:
gcc hello.cc -L /tmp -l app -I /tmp/firebase/include -o hello -lstdc++
I get this new error:
/usr/bin/ld: cannot find -lapp
collect2: error: ld returned 1 exit status
So, I think I'm not using -L and -l correctly, but not sure what exactly I'm doing wrong.
EDIT2
Fixed. Forgot I had renamed the lib file while trying to figure out the first issue. Once I named it back to libapp.a the app worked as expected.

Related

OSX: clang / ld says it can't find a static library even though it exists and all parameters are correct

I know it's going to be a silly mistake, and I'm aware that there are similar posts however none have helped so far, so I'm resorting to a new post.
I'm trying to compile the simplest GLFW hello world example.
In my project directory (I'm using vscode) there is a main.cpp file, and the glfw lib file I downloaded from glfw.org which I renamed to libglfw3.a (It's precompiled) I am using a MacBook Air M1.
I'm trying to compile it with this command:
clang -I /opt/homebrew/Cellar/glfw/3.3.7/include -L /Users//projects/ -l libglfw3.a main.cpp -o helloglfw
The output I get is:
ld: library not found for -llibglfw3.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm clearly dumb because for the life of me I can't see what I'm doing wrong...
Things I've tried:
linking to the homebrew version of glfw which contains a .dylib of glfw
tried linking libglfw3 (without the .a extension)
compiling to an object file and then linking with ld separately
using g++
Not using the -L flag and just pasting the full path into -l
Praying and restarting the pc just in case.
Waiting for it to fix itself
Some assistance would be appreciated.
OK SO! I fixed it. Apparently it works if there is NO SPACE between the -L and -l parameters.
so the command looks like this:
clang <source files> -I<Include Paths> -L<pathtolibs>-l<libs>
No clue why, but whatever I'm done...

Linking Paho C Mqtt library error in C++ Project

I'm trying to include the MQTT-C-Client-Library in a simple C++ project.
I have included the header file succesfully like this #include "MQTTClient.h". Compiling it in the linux terminal was printing this errors:
[xy#localhost mosquittoProject]$ sudo g++ *.cpp -o MQTTTest
/tmp/ccHn3s6m.o: In function `main':
mosquitto_test.cpp:(.text+0x11e): undefined reference to `MQTTClient_create'
mosquitto_test.cpp:(.text+0x13f): undefined reference to `MQTTClient_connect'
collect2: error: ld returned 1 exit status
I figured out that I need to link the library after some googling: Example MQTT Client Code not working C
Based on this question and answer I tried compiling it again like this:
sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
Which compiles fine but when running I get still an error.
Console commands and output:
[xy#localhost mosquittoProject]$ sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
[xy#localhost mosquittoProject]$ ./MQTTTest
./MQTTTest: error while loading shared libraries: libpaho-mqtt3c.so.1: cannot open shared object file: No such file or directory
I replaced the actual username by xy in this post.
What am I doing wrong here?
The problem looks like the library (libpaho-mqtt3c.so.1) is not on the library path.
It looks like you are linking against the build location of the library and have not installed it to the default system location (e.g. /usr/local/lib) by running sudo make install.
By default on Linux the runtime linker searches the locations listed in /etc/ld.so.conf and /etc/ld.so.conf.d. if you edit these remember to run sudo ldconfig to update the cache.
You can add the location of the library to the LD_LIBRARY_PATH environment variable e.g.:
$ LD_LIBRARY_PATH=/home/xy/Desktop/paho.mqtt.c/build/output/ ./MQTTTest

How to compile and link sqlite3 in a C++ program

I get the following response when I try to compile and link sqlite3 in a C++ program.
I use Eclipse Mars for C++
g++ "-LC:\Sqlite\sqlite3.h" -o Stryktips.exe "src\Stryktips.o" "-lC:\Sqlite\sqlite3.h"
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../.
./../x86_64-w64-mingw32/bin/ld.exe:
cannot find -lC:\Sqlite\sqlite3.h
collect2.exe: error: ld returned 1 exit status
C:\Sqlite\sqlite3.h is a header file. And as such you should #include it, not link with it. You should link with a file ending with .lib or .a
And the -L option is to add a directory path, not a file.
So your linker command like should look something like this:
g++ -o Stryktips.exe "src\Stryktips.o" -LC:\Sqlite -lsqlite
That will tell the linker that libraries can be found in the C:\Sqlite directory, and that it should link with the sqlite library.

/usr/bin/ld cannot find -l<nameOfLibrary>

I have written a few programs and while trying to compile them using g++,as thus,
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter
I keep getting the following error:
/usr/bin/ld: cannot find -lfwlib32
collect2: error: ld returned 1 exit status
fwlib32.h is the library I am trying to include. The shared object file libfwlib32.so is present in /usr/local/lib as well as /usr/lib. But I am unable link to it. I have tried all the solutions offered by similar questions including
$ export LIBRARY_PATH=/usr/local/lib/
$ export LD_LIBRARY_PATH=/usr/local/lib
I have done the above for /usr/lib as well, but still the same error.
I have tried using the -L option in the command line but I still get the error.
I even created a new folder called lib, pasted libfwlib32.so.1.0.1 into it and ran
$ ln -s ~/lib/libfwlib32.so.1.0.1 ~/lib/libfwlib32.so
on the console to create a new .so file and gave ~/lib as argument to -L option on the command line. It made no difference. I am at the point of tearing my hair out so any help will be appreciated.
Thanks alot!
You should put -l option in the very last as:
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -o adapter -lfwlib32 -lpthread
Note: Please make sure that all the header and source file are in the same folder.
Note that specifying -L~/lib won't work as the ~ will not be expanded by the shell. Also you can't add a space between -L and ~/lib. Instead you must specify it as a relative or absolute path.
Have you checked that the libfwlib32.so symlink exists in /usr/local/lib (or /usr/lib) in addition to the libfwlib32.so.1.0.1 file?
Another possibility is that the library is the wrong architecture (ie. 32-bit while your system is 64-bit), but then ld should print a message about skipping incompatible library. You can check the architecture of the library by running 'file libfwlib32.so.1.0.1'.
The error message suggests that -lfwlib32 is being interpreted as a filename not as a -l parameter. Put all the parameters before the files to be compiled
g++ -m32 -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp
As has been pointed out by #Erik Johannessen, libfwlib32.so is a 32bit library, so you need to add -m32 to build a 32bit executable.

Add BASS library in Code::Blocks on Linux

Can somebody help me with adding BASS library in Code::Blocks on Ubuntu? But step by step. I tried many things and I can't do this... I have "bass24-linux.zip" file and what now? Please help me as soon as possible!
EDIT:
I will tell you what Im doing:
Unzipping "bass24-linux.zip" on Desktop.
Copying "bass.h" to /usr/include
Copying "libbass.so" to /usr/lib
In Code Blocks going to Settings->Compiler->Search directories
Setting compiler path to /usr/include (where bass.h is)
Setting linker path to /usr/lib (where libbass.so is)
Then going to the project build options->Linker settings and set path ../../../../../usr/lib/libbass.so;
Then I'm writing a simple program, for example:
#include <iostream>
int main(){
}
And then I see error:
g++ -L/usr/lib -o bin/Debug/LightBulb obj/Debug/main.o ../../../../../usr/lib/libbass.so ../../../../../usr/lib/libbass.so: error adding symbols: File in wrong format collect2: error: ld returned 1 exit status