Linking static library to .cpp file

I have a libcx3d.a which contains my VrmlParser class and other classes which are used by VrmlParser. I have a main.cpp which does this :

VrmlParser vp = new VrmlParser();
double **VOB = vp.getVOB();

When I compile using g++ main.cpp -o main -L. -lcx3d, I get the following errors :

'VrmlParser' was not declared in this scope.
expected ';' before vp.
'vp' was not declared in this scope.

There is a header file called "VrmlParser.h" in the static library. Should I include this header file in main.cpp ? If so, will include "VrmlParser.h" work ? I have the .a and .cpp in the same directory. I can't find the header file for the static library.

What should I do ?
Last edited on
Did you add the library's path name to the linker options?
I have a directory called Temp which contains the libcx3d.a and main.cpp files. Please tell how should I compile. Sorry for being a noob.
> When I compile using g++ main.cpp -o main -L. -lcx3d, I get the following errors :
> 'VrmlParser' was not declared in this scope. ....

Include the header for the library. For instance, if it is vrmlparser.h
#include "vrmlparser.h"
I added the following things in main.cpp :

include <path_to_VrmlParser.h>
using namespace CyberX3D; // this is used by the static library

Then I did g++ -c main.cpp -I/path_to_header_files

main.o gets created. Then I do g++ -o main main.o -L. -lcx3d and errors come up. The errors are :

main.cpp:(.text+0x18): undefined reference to `CyberX3D::VrmlParser::VrmlParser()'

main.cpp:(.text+0x20): undefined reference to `CyberX3D::VrmlParser::getVOB()'

What is going wrong ?
Last edited on
Assuming that libcx3d.a is in the same directory as main.o

> g++ -o main main.o libcx3d.a
I ran the above command and the following got printed :

main.o: In function `main':
main.cpp:(.text+0x18): undefined reference to `CyberX3D::VrmlParser::VrmlParser()'
main.cpp:(.text+0x20): undefined reference to `CyberX3D::VrmlParser::getVOB()'
collect2: ld returned 1 exit status
animesh644 wrote:
I have the .a and .cpp in the same directory.
JLBorges wrote:
Assuming that libcx3d.a is in the same directory as main.o
What does > nm -C -g libcx3d.a print out?
I solved the problem.

Initially, I built the static library through cygwin and transferred the libcx3d.a to the remote server on which I plan to run my application. When I built the library again on the server and then linked to it, it worked.

Thanks for your replies. :)
Topic archived. No new replies allowed.