Building a project in VS that depends on a static and dynamic library

Posted by fg nu on Programmers See other posts from Programmers or by fg nu
Published on 2012-10-13T13:11:46Z Indexed on 2012/10/13 15:53 UTC
Read the original article Hit count: 518

Noob noobin'. I would appreciate some very careful handholding in setting up an example in Visual Studio 2010 Professional where I am trying to build a project which links:

  • a previously built static library, for which the VS project folder is "C:\libjohnpaul\"
  • a previously built dynamic library, for which the VS project folder is "C:\libgeorgeringo\"

These are listed as Recipes 1.11, 1.12 and 1.13 in the C++ Cookbook. The project fails to compile for me with unresolved dependencies (see details below), and I can't figure out why.


Project 1: Static Library

The following are the header and source files that were compiled in this project. I was able to compile this project fine in VS2010, to the named standard library "libjohnpaul.lib" which lives in the folder ("C:/libjohnpaul/Release/").

// libjohnpaul/john.hpp
#ifndef JOHN_HPP_INCLUDED
#define JOHN_HPP_INCLUDED

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

#endif // JOHN_HPP_INCLUDED

// libjohnpaul/john.cpp
#include <iostream>
#include "john.hpp"

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

// libjohnpaul/paul.hpp
#ifndef PAUL_HPP_INCLUDED
#define PAUL_HPP_INCLUDED

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

#endif // PAUL_HPP_INCLUDED

// libjohnpaul/paul.cpp
#include <iostream>
#include "paul.hpp"

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

// libjohnpaul/johnpaul.hpp
#ifndef JOHNPAUL_HPP_INCLUDED
#define JOHNPAUL_HPP_INCLUDED

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

#endif // JOHNPAUL_HPP_INCLUDED

// libjohnpaul/johnpaul.cpp

#include "john.hpp"
#include "paul.hpp"
#include "johnpaul.hpp"

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

Project 2: Dynamic Library

Here are the header and source files for the second project, which also compiled fine with VS2010, and the "libgeorgeringo.dll" file lives in the directory "C:\libgeorgeringo\Debug".

// libgeorgeringo/george.hpp
#ifndef GEORGE_HPP_INCLUDED
#define GEORGE_HPP_INCLUDED

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

#endif // GEORGE_HPP_INCLUDED

// libgeorgeringo/george.cpp
#include <iostream>
#include "george.hpp"

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

// libgeorgeringo/ringo.hpp
#ifndef RINGO_HPP_INCLUDED
#define RINGO_HPP_INCLUDED

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

#endif // RINGO_HPP_INCLUDED

// libgeorgeringo/ringo.cpp
#include <iostream>
#include "ringo.hpp"

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

// libgeorgeringo/georgeringo.hpp
#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

// libgeorgeringo/ georgeringo.cpp
#include "george.hpp"
#include "ringo.hpp"
#include "georgeringo.hpp"

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

Project 3: Executable that depends on the previous libraries

Lastly, I try to link the aforecompiled static and dynamic libraries into one project called "helloBeatlesII" which has the project directory "C:\helloBeatlesII" (note that this directory does not nest the other project directories).

The linking process that I did is described below:

  • To the "helloBeatlesII" solution, I added the solutions "libjohnpaul" and "libgeorgeringo";
  • then I changed the properties of the "helloBeatlesII" project to additionally point to the include directories of the other two projects on which it depends ("C:\libgeorgeringo\libgeorgeringo" & "C:\libjohnpaul\libjohnpaul");
  • added "libgeorgeringo" and "libjohnpaul" to the project dependencies of the "helloBeatlesII" project and made sure that the "helloBeatlesII" project was built last.

Trying to compile this project gives me the following unsuccessful build:

1>------ Build started: Project: helloBeatlesII, Configuration: Debug Win32 ------ 1>Build started 10/13/2012 5:48:32 PM.

1>InitializeBuildStatus: 1> Touching "Debug\helloBeatlesII.unsuccessfulbuild". 1>ClCompile: 1> helloBeatles.cpp 1>ManifestResourceCompile: 1> All outputs are up-to-date. 1>helloBeatles.obj : error LNK2019: unresolved external symbol "void __cdecl georgeringo(void)" (?georgeringo@@YAXXZ) referenced in function _main 1>helloBeatles.obj : error LNK2019: unresolved external symbol "void __cdecl johnpaul(void)" (?johnpaul@@YAXXZ) referenced in function _main 1>E:\programming\cpp\vs-projects\cpp-cookbook\helloBeatlesII\Debug\helloBeatlesII.exe : fatal error LNK1120: 2 unresolved externals 1> 1>Build FAILED. 1>

1>Time Elapsed 00:00:01.34 ========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

At this point I decided to call in the cavalry. I am new to VS2010, so in all likelihood I am missing something straightforward.

© Programmers or respective owner

Related posts about c++

Related posts about visual-studio