Ubuntu – How to use the GTK+ development libraries in Ubuntu

cgccgtkgtk3programming

I've been programming for about a year and a half on Windows and I've just started getting used to the way things work on that platform, but now I'm interested in development on Linux and I've been having a tough time getting used to the workflow, lack of IDE, and command line compilation with GCC. I'm running Xubuntu 13.10 64bit, by the way..

To test the waters, I did what many people do and wrote a quick C++ 'hello world' program.

#include <iostream>

int main()
{
    std::cout << "Hello!\n";
    return 0;
}

My first issue is; this program seems to get a compilation error when I try to compile it using GCC, but it compiles fine when I use the G++ compiler:

gcc hello.cpp -o hello
/tmp/ccbmUmzf.o: In function `main':
hello.cpp:(.text+0xa): undefined reference to `std::cout'
hello.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccbmUmzf.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

What's going on here, is GCC failing to recognize that my code is C++ or something?

My second issue is with trying to use libraries: I was under the impression that the GTK+3.0 development libraries come included with (X)ubuntu, but when I try to follow the simple tutorial here, i get the following error from G++:

g++ hello.cpp -o hello
hello.cpp:1:21: fatal error: gtk/gtk.h: No such file or directory
 #include <gtk/gtk.h>
                     ^
compilation terminated.

So then, I interpreted that to mean that the libraries for GTK+3.0 aren't installed.. So I checked apt-get, and I didn't really see anything that looked like what I needed.. Do i have to build GTK+3.0 from source, or is there some PPA somewhere that I need??

These issues are really frustrating to me.. I know that a lot of people swear by the GNU/Linux development workflow, but as someone who is used to working in Visual Studio, I'm finding the process to be somewhat opaque and unintuitive. Right now I'm using Mousepad on Xubuntu to edit my code, and while it has color-coded syntax, it seems to do very little beyond that. I know that eventually I'll have to deal with multiple files and creating makefiles and, at this point, I'm dreading it!

Whatever help you can give to a novice programmer and total Linux/GNU noob is very welcome!

Best Answer

Advice

First of all, you don't have to settle for no IDE. Install Eclipse, it's available in the Ubuntu Software Centre. You also want to install CDT for Eclipse which gives you C/C++ support. Second I'd very much advise you to focus on C++ and Qt instead of C and GTK. Qt is a great cross-platform framework that uses C++ and QML (a JavaScript-like language for UI), and Ubuntu is moving more and more towards using Qt for their future desktop and mobile development. These tools will allow you not only to create beautiful applications for Ubuntu desktop but also for Android, Windows, Mac, BlackBerry and other OSes. An excellent Qt-specific IDE is QtCreator, also available in the Ubuntu Software Center, you should check it out along some Qt tutorials.

Answer

To answer you question directly, even though I strongly recommend doing what I outlined above, you likely need the -dev versions of the libraries you use. For GTK 3, you likely have to:

sudo apt-get install libgtk-3-dev

That should get you all GTK 3 headers.