Inclusion error in Visual Studio Code on Linux
SolvedYour help will be very valuable to me. Thank you.
4 answers
Hello,
Which Linux distribution have you installed? How did you proceed to install the library? Under Debian and derived distributions (Ubuntu, Mint, ...) the package to install seems to be libsfml-dev :
sudo apt update sudo apt install g++ libsfml-dev
The library headers are deployed in /usr/include/SFML, as shown by the package contents. You can find in this list the file /usr/include/SFML/Graphics.hpp which you include in your code. Since /usr/include is a standard directory, your compiler (I assume g++) is supposed to find your headers directly without extra options (no need for the -I option).
However, you must still link your program with the libraries it depends on. Also according to the package contents the libraries are:
/usr/lib/x86_64-linux-gnu/libsfml-audio.so /usr/lib/x86_64-linux-gnu/libsfml-graphics.so /usr/lib/x86_64-linux-gnu/libsfml-network.so /usr/lib/x86_64-linux-gnu/libsfml-system.so /usr/lib/x86_64-linux-gnu/libsfml-window.so Again, /usr/lib/x86_64-linux-gnu is a standard directory, so you don't need to specify in which folder the compiler should look for the libraries (options -L and/or -Wl,R). But you still need to tell the compiler which libraries to link with using the -l option. Linking the program to libtoto.so is written as -ltoto during compilation, so in your case I would say your program compiles like this (some libraries listed below are not necessary as we will see just after):
g++ main.cpp -o mon_application -lsfml-audio -lsfml-graphics -lsfml-network -lsfml-system -lsfml-window
Now, if we look at this tutorial we can improve the previous compilation command:
g++ main.cpp -o mon_application -lsfml-graphics -lsfml-window -lsfml-system
Then, your program will eventually consist of several C++ files and each of them will have to be compiled individually (option -c, to compile a ".cpp" into a ".o") and then assembled with a final compilation command (which includes all the ".o" files and all the linkage options). It quickly becomes tedious to do by hand and you’ll want to use a Makefile to compile the program with the command make all and sudo make install (this command means: read the Makefile in the current folder and execute the targets all and install).
Nowadays, people rarely write the Makefile themselves (that happens on very small projects when you're in a rush, but it's not ideal, especially because the Makefile you write depends on the compiler, the operating system, etc.). In earlier times tools like automake were used but it's not easy to set up. Anyway, for all these reasons, I recommend you opt for cmake. If you develop with kdevelop (and I suppose anjuta too?), it is also possible to initialize your project to use cmake. Given a CMakeLists.txt file, the cmake command produces a Makefile conforming to your installation, which you can then use via the make command. Typically, we create a build folder at the project root and build everything there. From that folder you then run:
cmake .. make all sudo make install
Good luck
Hello,
Please avoid screenshots (not very eco-friendly and not practical) when a simple copy-paste and a code snippet sharing would suffice.
Installation
In your case you only need to explicitly install g++ (since it is C++), gcc is for pure C code. Similarly clang is another C compiler you don’t need. Thus this command would have sufficed:
sudo apt update sudo apt install g++ libsfml-dev
Compiler
The compiler indicated in your project configuration should be g++ (not gcc). If you use a C compiler to compile C++, it will not work.
Example :
toto.cpp
#include <iostream> int main() { std::cout << "hello" << std::endl; return 0; } Compilation with gcc toto.cpp :
(mando@silk) (~) $ gcc toto.cpp /usr/bin/ld: /tmp/ccbEYG74.o: warning: relocation against “ _ZSt4cout ” in text section “.text” /usr/bin/ld: /tmp/ccbEYG74.o: in function “main”: toto.cpp:(.text+0x11): undefined reference to “std::cout” /usr/bin/ld: toto.cpp:(.text+0x19): undefined reference to “std::basic_ostream<char std::char_traits=""> >& std::operator<< <:char_traits> >(std::basic_ostream<char std::char_traits=""> >&, char const*)” /usr/bin/ld: toto.cpp:(.text+0x20): undefined reference to “std::basic_ostream<char std::char_traits=""> >& std::endl<char std::char_traits=""> >(std::basic_ostream<char std::char_traits=""> >&)" /usr/bin/ld: toto.cpp:(.text+0x2b): undefined reference to “std::ostream::operator<<(std::ostream& (*)(std::ostream&))” /usr/bin/ld: /tmp/ccbEYG74.o: in function “__static_initialization_and_destruction_0(int, int)”: toto.cpp:(.text+0x5e): undefined reference to “std::ios_base::Init::Init()” /usr/bin/ld: toto.cpp:(.text+0x79): undefined reference to “std::ios_base::Init::~Init()” /usr/bin/ld: warning: creating DT_TEXTREL in a PIE collect2: error: ld returned 1 exit status</char></char></char></char></:char_traits></char> Compilation with g++ toto.cpp : no error
Including headers
The includePath option corresponds to the -I option in gcc/g++. You can keep it, but it only makes it easier to include the headers involved in your source code (not external headers, like SFML headers). You could specify that headers should be looked for in /usr/include, but normally that should be handled directly by g++, without any special option.
If your project is well configured, your IDE should not underline in red the includes.
Adding external libraries
I assume you can add in this configuration file the libraries you link to. I invite you to look at this discussion.
cmake
In any case, this configuration file depends on your IDE (Visual Studio). This means that if you redistribute your code, unless other developers also use Visual Studio and you have shared the project's configuration file, they will also have trouble compiling your project. The “proper” way to proceed is to use cmake and create a cmake-based project in your IDE (which is apparently supported in Visual Studio, see this link).
Personally, I would start from healthy foundations (namely: ensure your project compiles with cmake, then create in your IDE a cmake-based project). This is supposed to solve all your problems (because at worst you can compile from the command line). Indeed, cmake lets you specify everything (compiler, header inclusion, external libraries, etc).
If you opt for this method, you can base yourself on this tutorial. To install the relevant packages under Debian (or a derivative distribution) :
sudo apt update sudo apt install cmake cmake-gui-qt
Good luck
Thanks for the explanations, @mamiemando. On my PC, I installed the distribution "Linux pop-os 6.2.6". Before installing the library, I installed the C++ compilers with:
sudo apt install gcc g++ clang gdb
Then in Visual Studio Code, I added C/C++ IntelliSense. For the SFML library, I use the command to install it:
sudo apt-get install libsfml-dev
In the ".json" properties file, I noticed that the active compiler is "gcc"; I tried changing it to "g++" to see if the issue is resolved, but that isn’t the case.
I imported below a screenshot of the configuration to show what I’m saying. Otherwise, is there a way to link my program with the libraries without going through command lines?
You’re welcome :-)
I pasted a screenshot of the configuration below to show what I’m talking about. Otherwise, is there a way to link my program with the libraries without going through the command lines?
In theory, your IDE is supposed to allow you to compile using cmake without having to type the commands it entails yourself. I suppose that, since the topic has been marked as resolved, you’ve managed to do it?
