LinuxQuestions.org
Visit Jeremy's Blog.
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 09-02-2008, 01:37 PM   #1
neilcpp
Member
 
Registered: Jul 2003
Location: England
Distribution: Debian Jessie, FreeBSD 10.1 anything *nix to get my fix
Posts: 329

Rep: Reputation: Disabled
How to automatically get g++ and ld to look into library?


i want to be able to type a simple compile command like : g++ -o myprogram mycode.cpp for my program to compile.
however, my mycode.cpp depends on a library.

1 the library is installed in /usr/local/include/foo-library-0.3/foo-library/

2.the library is also installed in /usr/local/lib/ which has entries for libfoo.a, libfoo.la, libfoo.so etc.

If i try to compile my program, i get error that the headers for foo.h etc are not declared and there is no such file or directory.

What do I need to do to get this program to compile? At the lowest level, i want to type in my compile commands myself. Do I need to change the paths for my shell, linker and g++?
I have tried reading stuff about 'LD_LIBRARY_PATH' & compiler flags etc, but i dont understand them. Can you give me sure fire entries that I should add to set my paths right if i need to do this?
 
Old 09-02-2008, 03:31 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
g++ -o myprogram -lfoo -I/usr/local/include/foo-library-0.3/foo-library -L/usr/local/lib mycode.cpp
I don't know how you include your header files though, is it like <foo-library-0.3/foo-library/foo.h> or <foo.h> ?

Last edited by keefaz; 09-02-2008 at 03:35 PM.
 
Old 09-02-2008, 03:41 PM   #3
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by neilcpp View Post
[...] I have tried reading stuff about 'LD_LIBRARY_PATH' & compiler flags etc, but i dont understand them.[...]
That may be a bit of a problem.
Understanding preprocessor directives, linker options and makefiles is a rather fundamental part of learning to develop programs.

I'm not sure anyone can really give you explicit command-line options for your case without more information but, in general, you can specify directories to be searched for header files with the -I option, and directories to be searched for libraries with the -L option, and specific libraries to be linked with the -l option.

Of course, this assumes that you have specified the proper preprocessor directives in your source files so it knows what header files to include.

There are other options than the command line if you wish to include directories in the linker and/or preprocessor search paths. ldconfig will help you set some defaults for library paths, for instance.
 
Old 09-02-2008, 06:14 PM   #4
neilcpp
Member
 
Registered: Jul 2003
Location: England
Distribution: Debian Jessie, FreeBSD 10.1 anything *nix to get my fix
Posts: 329

Original Poster
Rep: Reputation: Disabled
Is this error due to the libarary or wrong linking+compile commands?

thanks guys, this is what i have tried:

$ g++ -o ttt -I/usr/local/include/ClanLib-0.8/ -L/usr/local/lib TicTacToe.cpp
In file included from /usr/local/include/ClanLib-0.8/ClanLib/display.h:61,
from TicTacToeApp.h:4,
from TicTacToe.cpp:2:
/usr/local/include/ClanLib-0.8/ClanLib/Display/keys.h:352:24: error: X11/keysym.h: No such file or directory
In file included from TicTacToeApp.h:5,
from TicTacToe.cpp:2:
/usr/local/include/ClanLib-0.8/ClanLib/gl.h:53:19: error: GL/gl.h: No such file or directory
/usr/local/include/ClanLib-0.8/ClanLib/gl.h:54:20: error: GL/glu.h: No such file or directory
In file included from /usr/local/include/ClanLib-0.8/ClanLib/gl.h:59,
from TicTacToeApp.h:5,
from TicTacToe.cpp:2:
/usr/local/include/ClanLib-0.8/ClanLib/GL/opengl_window.h:56:22: error: X11/Xlib.h: No such file or directory
In file included from /usr/local/include/ClanLib-0.8/ClanLib/gl.h:59,
from TicTacToeApp.h:5,
from TicTacToe.cpp:2:
/usr/local/include/ClanLib-0.8/ClanLib/GL/opengl_window.h:101: error: ISO C++ forbids declaration of 'Display' with no type
/usr/local/include/ClanLib-0.8/ClanLib/GL/opengl_window.h:101: error: expected ';' before '*' token
$


Whats wrong here? someone said i probably need to include other libaries as well - but that does not make sense to me.

Also Is there a way to setup anjuta ide to do this automatically?

--------------full code listing---------------
// the .h file)
// Include necessary CL header files
#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
#include <ClanLib/application.h>

class TicTacToeApp : public CL_ClanApplication {

public:
TicTacToeApp();
~TicTacToeApp();

virtual int main(int, char **);
};
-----------------source code----------------
// Implementation (for the .cpp file)
#include "TicTacToeApp.h"

// This application instance is required or the app will not run
TicTacToeApp applicationInstance;

// Constructor
TicTacToeApp::TicTacToeApp() {}

// Destructor
TicTacToeApp::~TicTacToeApp() {}

int TicTacToeApp::main(int, char **) {

// Create a console window for text-output if not available
CL_ConsoleWindow console("TicTacToeApp Console");
console.redirect_stdio();

try {
// CL initialization functions
// These must be called or CL functions will not work
// Also, SetupCore must be init()'ed first and denit()'ed last
CL_SetupCore::init();
CL_SetupDisplay::init();
CL_SetupGL::init();

// Set display mode
CL_DisplayWindow window("TicTacToeApp", 500, 450);

// CL deinitialization functions
CL_SetupGL::deinit();
CL_SetupDisplay::deinit();
CL_SetupCore::deinit();
} catch (CL_Error err) {
std::cout << "Exception caught: " << err.message.c_str() << std::endl;

// Display console close message and wait for a key
console.display_close_message();
}

return 0;
 
Old 09-02-2008, 07:01 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
g++ -o TicTacToe `clanlib-config --cflags --libs` TicTacToe.cpp
assuming you have the clanlib-config program installed (should be if you have the lib)
 
Old 09-02-2008, 08:06 PM   #6
neilcpp
Member
 
Registered: Jul 2003
Location: England
Distribution: Debian Jessie, FreeBSD 10.1 anything *nix to get my fix
Posts: 329

Original Poster
Rep: Reputation: Disabled
this didnt work. the script clanlib-config was afaik for clanlib 0-6 only. my version is 0.8. but this is a real simple 'example' program and i really do need to be able to compile it by hand ideally.
i also going to use anjuta ide - ive made a start reading the manual but unless i know the how & why of the command line syntax - i cant even have much hope of setting up anjuta to automatically do builds.
thanks for trying to help anyway.
 
Old 09-03-2008, 03:42 PM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
you really ought to deploy a makefile. Life is much simpler once you have done that.

Failing that, you should study the utility pkg-config. If the library you need has a .pc file (usually in /usr/lib/pkgconfig or /usr/local/lib/pkgconfig) then you can include all the libs like this (note that I am using backquotes - the unshifted ~ key):

g++ -o myprogram `pkg-config --cflags --libs myneededlib` myprogram.c

Note that "myneededlib" would be known on disk as "libmyneededlib.so" - you would skip the initial lib and all suffixes.

This will pick up all your dependencies and give you a good compile IF the library has a proper .pc file (most do).
 
  


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
list library function of a shared library .so powah Linux - General 7 10-25-2011 04:47 AM
Making a static library from a given shared library vro Programming 1 07-27-2007 04:07 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
what is the function library of the basic graphics library in rethat9.0? zerwolve Red Hat 0 04-29-2004 09:18 PM

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

All times are GMT -5. The time now is 09:51 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