Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello how can i fixes this error

[Error] SFML/Network.hpp: No such file or directory

The code:


#include <iostream>
#include <SFML/Network.hpp>
#include <sstream>
#include <string>
#include <vector>

static bool port_is_open(const std::string& address, int port)
{
    return (sf::TcpSocket().connect(address, port) == sf::Socket::Done);
}


static std::vector<std::string> split(const std::string& string,
                                      char delimiter = ' ',
                                      bool allow_empty = false)
{
    std::vector<std::string> tokens;
    std::stringstream sstream(string);
    std::string token;
    while (std::getline(sstream, token, delimiter)) {
        if (allow_empty || token.size() > 0)
            tokens.push_back(token);
    }
    return tokens;
}

static int string_to_int(const std::string& string)
{
    std::stringstream sstream(string);
    int i;
    sstream >> i;
    return i;
}

template <typename T>
static void swap(T& a, T& b)
{
    T c = a;
    a = b;
    b = c;
}

template <typename T>
static std::vector<T> range(T min, T max)
{
    if (min > max)
        swap(min, max);
    if (min == max)
        return std::vector<T>(1, min);
    std::vector<T> values;
    for (; min <= max; ++min)
        values.push_back(min);
    return values;
}

static std::vector<int> parse_ports_list(const std::string& list)
{
    std::vector<int> ports;
    for (const std::string& token : split(list, ',')) {
        std::vector<std::string> strrange = split(token, '-');
        switch (strrange.size()) {
            case 0: ports.push_back(string_to_int(token));       break;
            case 1: ports.push_back(string_to_int(strrange[0])); break;
            case 2:
            {
                int min = string_to_int(strrange[0]),
                    max = string_to_int(strrange[1]);
                for (int port : range(min, max))
                    ports.push_back(port);
                break;
            }
            default:
                break;
        }
    }
    return ports;
}

int main()
{
    std::string address;
    std::string port_list;
    std::vector<int> ports;
    std::cout << "Address: " << std::flush;
    std::getline(std::cin, address);
    std::cout << "Port: " << std::flush;
    std::getline(std::cin, port_list);
    ports = parse_ports_list(port_list);
    std::cout << "Scanning " << address << "...\n";
    for (int port : ports) {
        std::cout << "Port " << port << " : ";
        if (port_is_open(address, port))
            std::cout << "OPEN\n";
        else
            std::cout << "CLOSED\n";
    }
    std::cout << std::flush;
    return 0;
}


What I have tried:

should he be given a path? ...
#include <SFML/Network.hpp>
Posted
Updated 2-Nov-22 7:55am

The message is clear, the compiler cannot find that file or directory. Assuming this is some third-party header, you need to provide the full path, or add its location to the compiler search path.
 
Share this answer
 
If the SFML code is in the current directory, then use #include "SFML/Network.hpp" When you use #include <myfile.h> the compile only searches the System directories (e.g. /usr/include, /usr/local/include, some others specific to the compiler) and those added to the search list using -I /path/to/include. When you use double-quotes for an #include file, then the compiler will also search the current directory.

If you know what the explicit path to the headers is you can do #include "/some/path/SFML/Network.hpp" You can also tell the compiler to search a path relative to the current compilation module. e.g. #include "../../SFML/Network.hpp"

If you want to add to the compiler search path (-I option), then you'll have to consult the documentation for your build environment to find out how to do that.
 
Share this answer
 
Comments
Rick York 2-Nov-22 13:56pm    
Sorry, I hadn't seen your solution when I wrote mine which looks rather similar.
k5054 2-Nov-22 14:29pm    
I added mine after yours, but thought the OP should know the difference between #include <file.h> and #include "file.h", etc
Richard is correct. It is important to understand the difference between using angle brackets (<>) and quotes with include directives. The quotes will look for files in the local directory first, which is where the source file is, and then in the compiler's include directory search path. The angle brackets cause it to look only in the compiler's include directory search path. This will determine where the compiler looks for the file.

The statement
C++
#include <SFML/Network.hpp>
tells the compiler to look in the subdirectory SFML underneath one of its standard include directories. If that is not where you have that header then you probably need to use the double quotes with it, the ""s.

You can also add dots to specify the relative path as in :
C++
#include "../../SFML/Network.hpp"
That tells the compiler to look two levels up and then in the SFML subdirectory of that level. You would use that if you had a directory structure like this :
-Dev--+--MyProject--+--Source
      |
      +--SFML
and your code files were in Source. Two levels up from Source is Dev and Network.hpp would be in the SFML subdirectory of Dev.

FWIW, this is essentially what my development directory organization looks like.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900