fpagliughi / sockpp

Modern C++ socket library.
BSD 3-Clause "New" or "Revised" License
792 stars 126 forks source link

Compile with MinGW-64 (G++) #66

Open ryanwe1ss opened 2 years ago

ryanwe1ss commented 2 years ago

Hi fpagliughi,

Would you happen to know how to compile a sockpp included CPP file properly with G++? I've tried many including: g++ main.cpp -o main -I "sockpp/include/" -L "sockpp/build/Debug/sockpp-static.lib"

Including both the path and the linker file location. The program compiles properly when I have the actual include statement (example: #include <sockpp/tcp_connector.h>) only - but when trying to add a TCP connector (example: sockpp::tcp_connector conn({"localhost", 12345});) - it fails to compile.

fpagliughi commented 2 years ago

What platform are you trying this on? From the ".lib" I would assume a flavor of Windows?

I don't know Windows very well, and what limited experience I have with it is all with MSVC. So I'm not sure how much I can help. But...

It appears that you're working out of a build tree without installing it. In that case, the include files are not where you might expect. After install, the tree might look like:

usr
└── local
    ├── bin
    ├── include
    │   └── sockpp
    └── lib

So there's an include/sockpp (not just an include) So on the command line, you tell it to look in include ( -I /usr/local/include ) And in the source, you specify to look in a sockpp subdirectory: ( #include <sockpp/tcp_connector.h> )

Then, in Linux, the g++ switch '-L' tells the linker where to look for files, but the '-l' (lower-case L) tells it the library files. I would assume MinGW does the same?

So, all together, the command line is:

$ g++ tcpecho.cpp -o tcpecho -I /my/install/path/include -L /my/install/path/lib -lsockpp
ryanwe1ss commented 2 years ago

Yeah, i'm trying to compile it in windows in order to run on it which I am using as a client where I connect to it from a linux server.

I'm pretty sure it should be very similar due to my experience using the MinGW compiler on both windows and linux being about the same in terms of command line parameters.

I'll give that command a try in a bit, just out at the moment but thank you very much otherwise for your feedback!

ryanwe1ss commented 2 years ago

So I tried running this compile command: g++ main.cpp -o main -I "sockpp/include/" -L "sockpp/lib/sockpp-static.lib" -lsockpp

But my G++ compiler was unable to identify what "-lsockpp" is, outputting: cannot find -lsockpp (returned 1 exit status) on Windows. I was able to get it to work on Visual C++ but I couldn't get it to compile in release mode so it would not have to require the microsoft DLL's such as MSVCP140D.dll on any computer is runs on (self-contained). But so far this MinGW compiler I am using on Windows is installed the same way as it would be on linux and works as intended.

It is definitely a bit trickier on windows to perform these types of tasks but would you know how I could get MinGW to recognize what -lsockpp is?

fpagliughi commented 1 year ago

Sorry I never got back to this. I kept trying to figure out networking on Windows with MinGW, but I couldn't figure out the way different versions of the compiler define different macros. Like some versions seem to define _WIN32 and others don't?

So I gave up. Windows isn't a huge priority for me, and just getting MSVC working was enough of a chore. If you figured it out, let me know. I'm happy to put up instructions in the README or take whatever PR to make it work.

But, for completeness, with gcc, -L typically tells it where to look, so give that a directory. And -l tells it what library to link, with just the base file name, no extension. So in your case, together, it should just be:

g++ main.cpp -o main -I "sockpp/include" -L "sockpp/lib" -lsockpp-static