LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2009, 06:05 PM   #1
cbabbage
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Rep: Reputation: 0
Including c++ header files


I am a competent user of Ubuntu 8.04, competent in python but just starting to learn C++.
I want to start making GUIs using gtk. On my system the gtk header files,installed with the system are in /usr/include/gtk-2.0/gtk.

I've been attempting an extremely basic script to try to access the header files, like this:

#include <iostream>
using namespace std;
#include <gtk>
int main() {
cout << "DONE";
return 0;
}

When attempting to compile it, I've been using the -I flag to direct it to the gtk header files. However I get a long list of errors like this:

gtk/gtkwindow.h:218: error: ‘GtkWindow’ was not declared in this scope
gtk/gtkwindow.h:218: error: ‘window’ was not declared in this scope
gtk/gtkwindow.h:219: error: ‘gboolean’ was not declared in this scope
etc etc.

Is this a namespace issue? Can someone tell me how to go about including these gtk header files, or maybe point me to an existing web page somewhere which addresses the issue.

Thanks in advance,
cb.
 
Old 10-27-2009, 07:18 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by cbabbage View Post
I am a competent user of Ubuntu 8.04, competent in python but just starting to learn C++.
I want to start making GUIs using gtk. On my system the gtk header files,installed with the system are in /usr/include/gtk-2.0/gtk.

I've been attempting an extremely basic script to try to access the header files, like this:

#include <iostream>
using namespace std;
#include <gtk>
int main() {
cout << "DONE";
return 0;
}

When attempting to compile it, I've been using the -I flag to direct it to the gtk header files. However I get a long list of errors like this:

gtk/gtkwindow.h:218: error: ‘GtkWindow’ was not declared in this scope
gtk/gtkwindow.h:218: error: ‘window’ was not declared in this scope
gtk/gtkwindow.h:219: error: ‘gboolean’ was not declared in this scope
etc etc.

Is this a namespace issue? Can someone tell me how to go about including these gtk header files, or maybe point me to an existing web page somewhere which addresses the issue.

Thanks in advance,
cb.
C++ namespaces are stupid.
all includes should before any using statments
 
Old 10-27-2009, 07:26 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You have a path issue, not a namespace one (always look at the first error from the compiler).

The gtk libraries have lots of include and library directories (not just /usr/include/gtk2.0), so it is usually easier just to use pkg-config to generate these, eg:

Code:
gcc `pkg-config gtk+-2.0 --cflags --libs` test.c
If you are using the C++ wrappers for gtk, you can also use gtkmm-config, eg:

Code:
g++ `gtkmm-config --cflags --libs` test.cpp

Last edited by neonsignal; 10-27-2009 at 07:28 PM.
 
Old 10-27-2009, 07:31 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Namespaces shouldn't be causing problems here.. The first problem is that your include should be <gtk/gtk.h>, not just <gtk>. You should also use `pkg-config gtk+-2.0 --cflags` in CXXFLAGS and `pkg-config gtk+-2.0 --libs` in LDFLAGS. You are using at least a Makefile to build this, right?

I would personally avoid "using namespace std". It pollutes your global namespace with all the symbols from the standard library. A classic example is that if you try to name a counter with the rather reasonable "count", it'll clash with the std::count function.
 
Old 10-28-2009, 05:13 PM   #5
cbabbage
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Solved

Thanks. Problem solved using pkg-config.
CB.
 
Old 10-28-2009, 09:11 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by smeezekitty View Post
C++ namespaces are stupid.
all includes should before any using statments
This is wrong, #includes can appear after using statements. They can appear at any point in the file, however in some locations it may not make sense (such as within a function), but a #include is just a preprocessor directive which says expand the contents of this file here, so it is quite legitimate to have a #include contain some code and expand it at that point. Having said that this is not anything that I would encourage.

But in summary what this means is that #includes can appear after code, such as a class or function declaration. Occasionally it is necessary to include a #include after a class declaration to avoid circular references.
 
Old 10-28-2009, 09:36 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
it is quite legitimate to have a #include contain some code and expand it at that point
Indeed. I've used a macro-based trick to make it easy to dynamically load functions from shared objects. I've also heard of some code that uses perl to create a lookup table that's #included in the right spot. XPM files are also just C, so you can #include them right into an app.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenCV compilation problem (add directory for including header files) maikki Programming 2 01-07-2009 08:07 AM
including /usr/include/linux/ header files throwing errors in SLES 10.2 deedhnd Linux - Server 9 08-19-2008 08:08 AM
Including header file using HTML hoho4rd Programming 4 12-05-2006 11:20 AM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
How to write Makefile in 2.6 for including header files in another dir ? ashbalu Programming 0 10-19-2004 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration