LinuxQuestions.org
Review your favorite Linux distribution.
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 11-06-2007, 07:39 AM   #1
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Rep: Reputation: 15
How to link external c++ libraries?


Hi all:

I am a newbie in C++ in Linux.

I have just installed VTK libraries and all the demo programmes running well. I just want to try to compile a simple program but it seems that I can't link to the VTK libraries. I am running Eclipse for C++ as my IDE. The program is like:

Code:
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkConeSource.h"
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkPolyDataMapper.h"
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkRenderWindow.h"
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkCamera.h"
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkActor.h"
#include "/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkRenderer.h"

int main()
{

  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  

  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );


  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );


  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );

  int i;
  for (i = 0; i < 360; ++i)
    {
    // render the image
    renWin->Render();
    // rotate the active camera by one degree
    ren1->GetActiveCamera()->Azimuth( 1 );
    }
  

  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren1->Delete();
  renWin->Delete();

  return 0;
}
/local/d0p6/ming/VTK-library/include/vtk-5.0 is the path where the header files locate. But when I tried to compile the program,
it gave me

Code:
o(.text+0x1ed):../main.cpp:81: undefined reference to `vtkRenderer::AddActor(vtkProp*)'
./main.o(.text+0x22e):../main.cpp:89: undefined reference to `vtkRenderWindow::New()'
./main.o(.text+0x2aa):../main.cpp:102: undefined reference to `vtkRenderer::GetActiveCamera()'
./main.o(.text+0x2b3):../main.cpp:102: undefined reference to `vtkCamera::Azimuth(double)'
./main.o(.gnu.linkonce.t._ZN12vtkAlgorithm13GetOutputPortEv+0xf): In function `vtkAlgorithm::GetOutputPort()':
/local/d0p6/ming/VTK-library/include/vtk-5.0/vtkAlgorithm.h:289: undefined reference to `vtkAlgorithm::GetOutputPort(int)'
collect2: ld returned 1 exit status
make: *** [firstVTK] Error 1
Does anyone have any idea how to link the library to my program please?

Thanks a lot
 
Old 11-06-2007, 08:07 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There are two things:

1. Instead of putting the whole path to the include files in the #include line, it is more usual to put just the source file name, and then in the build options, pass an include search path to the compiler. If you are using gcc, this is done with the -I option.

2. When linking to external libraries, you must tell the linker what library defines the symbols which you have used. For example, the sin function is declared in the math.h header, so the source needs to #include this. The implementation of the sin function is found in the libm.a file, and so the parameter -lm must be passed to the linker stage. (the name of the library without the lib prefix and the .a or .so suffix). The linker will check some standard paths (described in the linker manual page), but if your library is installed in a non-standard location, you can provide a linker path option using -L.

For example, consider a program which calls a function called myfunc, declared in mylib.h and implemented in libmylib.a. If the mylib.h file is found in /home/matthew/include, and the libmylib.a file is found in /home/matthew/lib, and the program file, called "main.cpp" contains this:
Code:
#include "mylib.h"

int main()
{
   ...
}
The command to build the executable would be:
Code:
g++ -I/home/matthew/include -L/home/matthew/lib -lmylib main.cpp -o main
 
Old 11-06-2007, 08:12 AM   #3
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks Matthew. I will try this asap. But is there any clue of how to do this in Eclipse instead of command line? I am a windows man really don't like to remember that many options and parameters.
 
Old 11-06-2007, 08:43 AM   #4
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
The paths to the libraries have to be inserted in Eclipse under "Project => Options" (or was it "Properties"? well, something like that.
 
  


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
How to link libraries with g++ [KIA]aze Programming 1 04-13-2007 03:38 AM
link non-default libraries in KDevelop?? TLV Programming 3 12-14-2004 11:25 AM
Dynamic link libraries eshwar_ind Programming 1 05-09-2004 11:02 PM
Linux Version of Dynamic Link Libraries paul_eniki Programming 4 02-05-2004 09:25 AM
gaim trying to link static and shared libraries rose_bud4201 Linux - Software 0 09-28-2003 12:27 AM

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

All times are GMT -5. The time now is 01:01 PM.

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