C++ (socket) programming on Linux

Hello,

I read a little about socket programming and now want to use non-Boost Asio library for C++ socket programming using gcc on Linux (Ubuntu).
I'm confused by gcc and looked it up on the Web but still it's not clear for me. And since I want to use C++ on that I came here to ask. In fact a couple of questions:

1- I installed gcc version 7.1 on Ubuntu using terminal commands. And I assume gcc is a compiler, but how to code on that!!? For instance, on VS on Windows we easily create a project and write our C++ code on that and run it. But how to write a simple "hello world!" code on gcc? Does it have an IDE at all? If not then what environment should we use to write our code and then run it?

2- If I know how to write a simple code on gcc and run it, I will search for tuts to learn Asio on it too. But does programming generally on Linux either it's non-networking or networking/socket programming have benefits over IDEs on Windows? what are your preferences and reasons for them please?
I think the only profit would be that when I write C++ socket or non-socket programs on Linux, I will be more familiar with Linux. If I do that on Windows, I will be more familiar with Windows. do you agree?

Thanks so much in advance.
I assume gcc is a compiler, but how to code on that!!? For instance, on VS on Windows we easily create a project and write our C++ code on that and run it.
Does it have an IDE at all?

There are IDEs compatible with GCC, or other compilers. I don't really know which are the best, so I'll just link an article that has its own opinions.
http://www.linuxandubuntu.com/home/8-best-ides-or-code-editors-for-linux

I've used CodeBlocks on Windows, which also has a linux version. It's okay.

____________________________________________________________

But you don't need an IDE. Often times, a simple text editor with syntax highlighting paired with a terminal is all you need.

e.g. type
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello world\n";
}

into your text document, foo.cpp.

then call the compiler:
g++ foo.cpp -o foo

then run
./foo

Hopefully someone else can answer the sockets-related question.
Last edited on
And I assume gcc is a compiler, but how to code on that!!?


Yes it is a compiler and it can be used by all kinds of IDE, like Eclipse, KDevelop, CodeBlocks etc.

To use it from the shell, create a file with an editor like kwrite. If your file was hello.cpp, then compile like this:

g++ -std=c++17 -Wall -Wextra -pedantic-errors hello.cpp -o hello

g++ is the command for c++, the C command is gcc. gcc actually means gnu compiler collection, it can do other languages too.

then run the executable like this:

./hello

The ./ is kind of a security thing, it specifies to look in the current directory not in path environment variable.

It's worth reading the manual for gcc, there are a zillion options, but still worth it nonetheless.

Once you have a reasonable sized project you may want a make file, but using an IDE is easier.

Good Luck !!


Thank you both very much.
Kwrite was what I needed! :)

I prefer at least for the time being not to use IDEs and stick to shell and that simple but good text editor.

I used this "g++ foo.cpp -o foo" command. Is it good enough, at least for starting please?

And @Ganado, you talked about "a simple text editor with syntax highlighting paired with a terminal". Is it Kwrite or you meant something better please?


I hope someone else give answers to the networking part of the questions including this one too: Can I use this simple text editor for socket programming too.

I used this "g++ foo.cpp -o foo" command. Is it good enough, at least for starting please?

Only for the most basic of basic programming. You need to add switches to the command line for debugging and better error diagnostics along with telling the compiler what standard you wish to use.

g++ -g -Wall -Wextra -pedantic -pedantic-errors -vla -std=c++17 foo.cpp -o foo

There are quite a few more switches available, check out the documentation of the compiler for more information.

Can I use this simple text editor for socket programming too.

Yes. And don't forget to add the proper library switches for the socket library you're using.
Last edited on
I reasonably good IDE on Linux etc is kdevelop. I has code checkers and so on.

Once you're comfortable with your IDE, it's work learning how the compiler is run, how the program is linked (there's a sight variation between Unix and Windows), how to check your programs performance at runtime (heap use and so on).
Topic archived. No new replies allowed.