Linking libraries

I'm trying to find a way to compile a program that I have with a few external libraries. After some searching, I found an example in O'Reilly's C++ Cookbook.

I have it so that I have the following directory structure with the follwoing files in them:

hellobeatles/hellobeatles.cpp
georgeringo/libjohnpaul.a
johnpaul/libgeorgeringo.so

How would I compile hellobeatles.cpp linking with the other libraries (libjohnpaul.a and libgeorgeringo.so)?

The example in the book says to use:
g++ -o hellobeatles hellobeatles.o -L../johnpaul -L../georgeringo -ljohnpaul -lgeorgeringo

But this gives me the following error:
g++: hellobeatles.o: No such file or directory
g++: no input files

What have I done wrong? Do you have any other suggestions that I could try?
I have both MinGW and Cygwin setup on my Windows 7 machine.

Thank you!
the .a and .so files can be put in /usr/lib32 or 65 or something like that. Then you don't need to use absolute directory paths... or simply just name them -l[library name]

Also, there might be a problem simply because you're using a capitol L. The shell is funny about typecase and also commands can have different meanings or no meanings at all for capitol letters.

What also might be the probelm as well is you shouldn't have to use libjohnpaul.a as being linked... just the .so files(i'm not sure about this though.)

lmao is this a joke too? your filenames are funny...

I just read a bit more where your problem might be

First you have to type
gcc -c filename

That will turn it to an object file
Then you type
gcc -o [name of exe] [object file] -l[link names]
Last edited on
Thanks for the reply. I tried
gcc -c hellobeatles.cpp
and
g++ -c hellobeatles.cpp
first but I get this error:
hellobeatles.cpp:1:33: fatal error: johnpaul/johnpaul.hpp: No such file or directory
compilation terminated.

Lol the filenames are from the example in the O'Reilly C++ Cookbook. This is the contents of the files:

johnpaul/john.hpp
1
2
3
4
5
6
#ifndef JOHN_HPP_INCLUDED
#define JOHN_HPP_INCLUDED

void john( ); // Prints "John, "

#endif // JOHN_HPP_INCLUDED 


johnpaul/john.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "john.hpp"

void john( )
{
    std::cout << "John, ";
}


johnpaul/paul.hpp
1
2
3
4
5
6
#ifndef PAUL_HPP_INCLUDED
#define PAUL_HPP_INCLUDED

void paul( ); // Prints " Paul, "

#endif // PAUL_HPP_INCLUDED 


johnpaul/paul.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "paul.hpp"

void paul( )
{
    std::cout << "Paul, ";
}


johnpaul/johnpaul.hpp
1
2
3
4
5
6
#ifndef JOHNPAUL_HPP_INCLUDED
#define JOHNPAUL_HPP_INCLUDED

void johnpaul( ); // Prints "John, Paul, "

#endif // JOHNPAUL_HPP_INCLUDED 


johnpaul/johnpaul.cpp
1
2
3
4
5
6
7
8
9
#include "john.hpp"
#include "paul.hpp"
#include "johnpaul.hpp"

void johnpaul( )
{
    john( );
    paul( );
}


georgeringo/george.hpp
1
2
3
4
5
6
#ifndef GEORGE_HPP_INCLUDED
#define GEORGE_HPP_INCLUDED

void george( ); // Prints "George, "

#endif // GEORGE_HPP_INCLUDED 


georgeringo/george.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "george.hpp"

void george( )
{
    std::cout << "George, ";
}


georgeringo/ringo.hpp
1
2
3
4
5
6
#ifndef RINGO_HPP_INCLUDED
#define RINGO_HPP_INCLUDED

void ringo( ); // Prints "and Ringo\n"

#endif // RINGO_HPP_INCLUDED 


georgeringo/ringo.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "ringo.hpp"

void ringo( )
{
    std::cout << "and Ringo\n";
}


georgeringo/georgeringo.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef GEORGERINGO_HPP_INCLUDED
#define GEORGERINGO_HPP_INCLUDED

// define GEORGERINGO_DLL when building libgerogreringo.dll
# if defined(_WIN32) && !defined(__GNUC__)
#  ifdef GEORGERINGO_DLL
#   define GEORGERINGO_DECL _  _declspec(dllexport)
#  else
#   define GEORGERINGO_DECL _  _declspec(dllimport)
#  endif 
# endif // WIN32

#ifndef GEORGERINGO_DECL
# define GEORGERINGO_DECL
#endif

// Prints "George, and Ringo\n"
#ifdef __MWERKS__
# pragma export on
#endif

GEORGERINGO_DECL void georgeringo( ); 
#ifdef __MWERKS__
# pragma export off
#endif

#endif // GEORGERINGO_HPP_INCLUDED 


georgeringo/ georgeringo.cpp
1
2
3
4
5
6
7
8
9
#include "george.hpp"
#include "ringo.hpp"
#include "georgeringo.hpp"

void georgeringo( )
{
    george( );
    ringo( );
}


hellobeatles/ hellobeatles.cpp
1
2
3
4
5
6
7
8
9
#include "johnpaul/johnpaul.hpp"
#include " georgeringo/ georgeringo.hpp"

int main( )
{
    // Prints "John, Paul, George, and Ringo\n"
    johnpaul( );
    georgeringo( );
}


The files in the johnpaul folder make up a static library (libjohnpaul.a) and the files in the georgeringo folder make up a dynamic library (libgeorgeringo.so). The library files are created and seem to be fine.

I just don't know how to create an exe from hellobeatles.cpp and link up the library files.

Any help would be great!
Thank you :)
the filenames are from the example in the O'Reilly C++ Cookbook.

Don't they provide a makefile?

Or at least explain how to write one?

To fix

hellobeatles.cpp:1:33: fatal error: johnpaul/johnpaul.hpp: No such file or directory

you need to provide the compiler with a -I command line option, to tell it to check another directory for include files.

g++ -c -I.. hellobeatles.cpp

You need .. as the path here as you want the compiler to look in the parent directory for "johnpaul/johnpaul.hpp" and "georgeringo/ georgeringo.hpp" (I'd lose the space from your version)

(This does assume that hellobeatles, johnpaul, and georgeringo are all subdirs of the same dir.)

Andy

P.S. I use g++ for .cpp and gcc for .c (it prob doesn't really matter, but as it's there...)
Last edited on
i made a mistake too in what I said... apparently .so is for shared and dynamic link files.

.a is for static linked files.
Awesome another Beatles Fan :)
Thanks so much!! After giving it the -I file path, it worked fine! That was driving me nuts. The book goes into explaining Make files later on but I couldn't get this bit to work first.

Thanks again! :D
Topic archived. No new replies allowed.