Is there a way to link SFML libraries in VSCode Mac? - c++

Is there a reasonably easy to follow way to link SFML libraries with VSCode in macOS?
My case:
Using Mac
Using Clang with VSCode
Have Xcode installed
Am an amateur
Note : I am using clang and Mac

See my question and answer here: Manually link and compile C++ program with SFML (macOS)
In a nutshell:
download sfml for mac
copy include directory from extracted folder to your project directory
copy the dynamic library files to your project also, folder lib
in terminal type the g++ command and link to the dynamic libs
It will be something like this:
g++ main.cpp -o main -I include -L lib -l sfml-system -l sfml-window -l sfml-graphics -l sfml-audio -l sfml-network -Wl,-rpath ./lib

here's a boiler plate to link sfml in vs code:
https://github.com/andrew-r-king/sfml-vscode-boilerplate

If you have Macbook M1, the other answers won't work. The reason is because sfml from the website is compiled in x86_64, and you can't compile the libraries directly to arm64. So, you need sfml and pkg-config to be installed on Homebrew.
Command:
g++ main.cpp $(pkg-config --libs --cflags sfml-window sfml-system sfml-graphics) -o main
More detailed solution here: https://stackoverflow.com/a/53510642/16264548
If you don't wan to use pkg-config, then you can manually type in the locations:
main.cpp -I/opt/homebrew/Cellar/sfml/2.5.1_1/include -L/opt/homebrew/Cellar/sfml/2.5.1_1/lib -lsfml-window -lsfml-system -lsfml-graphics -o main

To enable the editor features, you can add library include files to C/C++ configurations:
Open Command Palette (⇧⌘P by default)
Type and select "C/C++: Edit Configurations (UI)"
In section "Include path" add a line: /your/path/to/sfml/include/*
In my case, the path is /usr/local/Cellar/sfml/2.5.1_1/include/*

Related

Cross-compiling c++ with sdl on linux

I'm on Arch Linux, and I have a C++ SDL2 program, contained in single main.cpp file, and I compile it for Linux with such command:
g++ main.cpp -lSDL2 -lSDL2_image
Now I wanna compile it for windows. Any advice on what should I do?
I suggest my own tool, quasi-msys2, which lets you reuse the precompiled SDL2 for MinGW provided by MSYS2 (and more).
Install Clang, LLD, make, wget, tar, zstd, gpg.
git clone https://github.com/HolyBlackCat/quasi-msys2
cd quasi-msys2/
make install _gcc _SDL2 _SDL2_image
env/shell.sh
win-clang++ main.cpp `pkg-config --cflags --libs sdl2 SDL2_image`
This should produce a.exe, which you can test using wine a.exe (or just ./a.exe, after running env/shell.sh).
How to do this manually:
For completeness, SDL2 itself distributes precompiled binaries for MinGW, meaning the manual setup is not hard. Any tutorial for MinGW should work.
Install MinGW from your package manager.
Download and unpack SDL2-devel-??-mingw.zip and SDL2_image-devel-??-mingw.zip.
Specify the paths to the directories with .a files using -L... and to .h files using -I.... Add -lmingw32 -lmingw32 -mwindows -lSDL2main -lSDL2 -lSDL2_image to the linker flags.
Follow this troubleshooting guide if you get stuck.
I'd go for cmake really.. SDL2 ships with a CMakeLists.txt and it's as simple as running this from your build folder.
cmake.exe ..
cmake.exe --build .
EDIT: if you want to cross-compile, you need MinGW and the addition of the mingw flags to the cmake generator
cmake \
-D CMAKE_C_COMPILER=/path/to/wingw/gcc \
-D CMAKE_CXX_COMPILER=/path/to/mingw/g++ \
-G "MinGW Makefiles" ..

Error loading SDL2 shared libraries while executing program on another pc

I'm trying to compile a program i made using SDL2 to work on others computers (or testing VM in this case).
I've been compiling it with what i think are the correct flags, e.g. g++ main.cpp -o main -lSDL2, however when i try executing it on another Ubuntu installation i get this error.
error while loading shared libraries: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory
From my understanding it's not a problem in my compiling but with how i expect it to work on another Linux installation; I've cross-compiled (using mingw32) and tested it (using a freshly installed VM) on Windows adding the correct dlls with the exe (seems to work fine) and I was expecting for it to work in a similar fashion.
What's the standard in this cases? Should i write a setup scripts to install the library dependencies on the target machine? Is there another way I'm not aware of? I've never released an application for Linux (nor Windows) and I'm struggling to find the resources to do things "the right way".
Thanks for everyone suggestions, I ended up settling for the easy way, compiling the "easy to install" libraries dynamically e.g.-lSDL2 and the others statically (checked the licenses and it should be fine) like so:
g++ main.cpp -o main -Wl,-Bdynamic -lSDL2 -lSDL2_image -lSDL2_ttf -Wl,-Bstatic -lSDL2_gfx -static-libgcc -static-libstdc++
I'll put in my documentation how to install the required SDL2 libraries.
I am not sure how familiar you are with pkg-config, but the output for sdl2 is this:
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
This was found from running this:
pkg-config --cflags --libs sdl2
Basically, you need to point to where SDL2 is located BEFORE you actually link to it.
The tool pkg-config is designed to tell you the information you need when you want to link to a package in Linux. You were linking with the library, but you forgot to tell GCC where the library is located.
If you want to compile you code, try this:
g++ main.cpp -o runme `pkg-config --cflags --libs sdl2`
This will automatically grab all of the flags that you need to compile with SDL2 included.
Oh, and you should note, ORDER MATTERS WHEN ADDING FLAGS AND LIBRARIES!!!
There are many questions on SO where the order of compiler options caused all of the problems. Do not be like those people. I suggest you search SO for more info on that.

Building Chilkat with a .sh file

I downloaded chilkat and I need to build it... I got 2 .sh files. The README says:
To build the C and C++ samples, first edit the c_sampleBuild.sh and
linkSample.sh scripts and set the "-L" option's path for the system libraries.
LinkSample.sh:
g++ -Wl,--enable-auto-import linkSample.cpp -o"linkSample.exe" -L. -libchilkat-9.5.0 -L/c/MinGW/lib -lcrypt32 -lws2_32 -ldnsapi
c_sampleBuild.sh:
#!/bin/bash -ev
gcc -c c_Sample.c -o"c_Sample.o"
g++ c_Sample.o -o"c_Sample.exe" -L. -lchilkat-9.5.0 -L/c/MinGW/lib -lcrypt32
-lws2_32 -ldnsapi
It's now clear how I should make that.... Help please :) Thanks
You haven't said what platform you are building on. But if you are using Windows + MinGW then:
The libraries for crypt32, ws2_32 and dnsapi can be downloaded from: here
For the g++ command, the -l (lowercase) option tells g++ which additional libraries you want to link against, and the -L (uppercase) option is used to tell g++ where to look for the libraries.
If you have a library file called libbar.a in the current folder (.) then you add the option (-L. -lbar)
Or, if you have a library in /path/to/foo/libbar.a then you add (-L/path/to/foo -lbar).
You will need to check the MinGW documentation for the locations of the system libraries, they are likely to be found in /lib or /usr/lib.

libxml/parser.h: in c++ ubuntu

Even though I have installed libxml++2.6-2 libxml++2.6-doc etc in my ubuntu 12.04 version again I am getting the below error
fatal error: libxml/parser.h: No such file or directory
I am using make for building the project
Kindly suggest any other libxml libraries which I need to install
libxml/parser.h is a part o libxml library, not libxml++
For any given library, you need development packages (the ones with names ending in -dev) in order to build applications using that library.
You need to pass additional flags to your compiler: xml2-config --cflags and to linker xml2-config --libs.
I don't have access to an Ubuntu system now, but: Maybe you need to install the libxml developer package? Maybe you only have the library but not the include file(s)?
Check in /usr/include, /usr/local/include, ... for the directory libxml and the file parser.h.
If you find the file, you may need to adapt your makefile so that the parent-directory is in the list of include paths, e.g.:
INC = -I/usr/local/include
g++ $(INC) ...
If you did not find the file: Check the available libxml packages for a developer package and install that.
Before Posting the answer THANKS to the people who have answered, but those answers were not worked for me
I have just copied libxml folder from the directory usr/lib/libxml2 and pasted in usr/lib directory and compiled my code it is not giving any error. It is working fine now.
Please read #el.pescado answer before reading this. I wanted to comment on that answer but felt the need to format my code better.
gcc -c <files to compile> `xml2-config --cflags` -o <object files>
gcc <object files> -L<libxml2 lib location> `xml2-config --libs` -o <binary file>
Assuming we have a file names xmltest.c that have code that included libxml2 header like #include <libxml/parser.h>, standard location of libxml2 shared library i.e. /usr/lib64/libxml2, the above code will evaluate like this:
gcc -c xmltest.c -I/usr/include/libxml2 -o xmltest.o
gcc xmltest.o -L/usr/lib64/libxml2 -lxml2 -lz -lm -o xmltest
A better idea is to put together a Makefile that does this automatically.

dbus - how to set include paths

On my system dbus headers are placed in /usr/include/dbus-1.0/dbus/ and dbus-arch-deps.h is other location (what seems to be strange): /usr/lib/x86_64-linux-gnu/dbus-1.0/include/dbus/dbus-arch-deps.h In my program I include #include<dbus-1.0/dbus/dbus.h>but in every header file which include others path looks like this: #include<dbus/xxx.h> I can copy dbus-arch-deps.h to /usr/include/dbus-1.0/dbus/ but how to fix paths in dbus headers ?
Your system likely has pkg-config installed.
g++ $(pkg-config --cflags dbus-1) main.c
Pkgconfig contains a database of linker/compiler/etc. flags that are required to use specific libraries. See man pkg-config for more info.
First of all you need to install and configure it properly.
You should try this command :
sudo apt-get -y install dbus libdbus-1-dev libdbus-glib-1-2 libdbus-glib-1-dev
Now, here is the Makefile that you should write for compiling :
all:
g++ dbus.cpp -I/usr/include/dbus-1.0 \
-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include \
-I/usr/include/glib-2.0 \
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
-ldbus-1 \
-ldbus-glib-1
Now, you may include files like dbus/dbus.h, dbus/dbus-glib.h, etc.
You don't need to copy files.
Simply add the path of where dbus is located to your include path when compiling using the I flag:
example:
g++ -Wall -I /usr/include/dbus-1.0/ -o main.o
By using the location of where dbus is located (in the standard location of /usr/include, you can reference the files like the following in your source code:
#include <dbus/xxx.h>
Similarly, if you have to link against dbus you'll have to append that path to the Libraries inclusion path like so:
g++ -Wall -I /usr/include/dbus-1.0/ -o main.o -L <dbus library path>
Where dbus library path is where the libraries ofdbus` live. To figure this out, consult the web, or search your system.
UPDATE:
To achieve that in Qt-Creator (which I've never used), perhaps the following can help:
How to add include path in Qt Creator?