[CMake] Linking problem

Sanatan Rai sanatan at gmail.com
Wed Jul 27 07:14:53 EDT 2011


Hi,
   This is a newbie question. So apologies in advance if this is covered
somewhere in the docs (I haven't been able to find a satisfactory explanation).

The issue is this:

* I have a library called (lib)atmath as per:

include_directories(${at_SOURCE_DIR})
add_library(atfwmath RngStream.cpp RngStream.h coin.cpp coin.hpp)
target_link_libraries(atfwmath)

Where coin.hpp:

#ifndef COIN_HPP_INCLUDED
#define COIN_HPP_INCLUDED
////////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include <fw/math/RngStream.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
class coin
{
private:
  static string name()
  {
    static unsigned long id(0);
    stringstream str;
    str << "Coin_" << (++id) << "_stream";
    return str.str();
  }
  double p;
  RngStream *rng;
  void init();
public:
  coin();
  coin(double _p);
  ~coin();
  bool toss();
  void reset(double _p);
};
#endif

and coin.cpp:

#include <fw/math/coin.hpp>
////////////////////////////////////////////////////////////////////////////////
coin::coin() : p(0.5), rng(0) { init();}
coin::coin(double _p) : p(_p), rng(0)
{
  if ((p < 0) || (p > 1))
    throw;
  init();
}
coin::~coin()
{
  delete rng, rng = 0;
}
void coin::init()
{
  rng = new RngStream(name().c_str());
}
bool coin::toss()
{
  double u(rng->RandU01());
  return (u >= p);
}
void coin::reset(double _p)
{
  if ((_p < 0) || (_p > 1)) throw;
  p = _p;
}


I use these classes in another library:

add_library(atidt STATIC idt.cpp)
target_link_libraries(atidt atmath boost_date_time boost_signals)


which is then linked to the final executable:
add_executable(bt bt.cpp)
target_link_libraries(bt atidt atmath boost_date_time boost_program_options)
target_link_libraries(bt "-Wl,-whole-archive -L../idt/ -latidt
-Wl,-no-whole-archive")

When I run make:

   * libatmath compiles fine.
   * libatidt complies fine.
   * when compiling bt, it complains about coin::coin, coin::reset etc
being undefined.
     Ie it would see it is not finding them in the linked libraries.
Even though:

     $ ar t libatmath.a
      RngStream.cpp.o
      coin.cpp.o

What am I missing here? It is also quite bizarre as it doesn't
complain about the declarations in
RngStream.h which are defined in RngStream.c.

Thanks in advance!

--Sanatan


-- 
Sanatan Rai


More information about the CMake mailing list