I have a static library "libCam.a" The library contains a Camera class object based on this code for Camera class
#include "Camera.h"
#include <optix_world.h>
#include "Arcball.h"
using namespace optix;
bool Camera::rotate( float dx, float dy )
{
static sutil::Arcball arcball;
const float2 a = make_float2(1.0, 2.0);
const float2 b = make_float2(3.0, 4.0);
rotMatrix = arcball.rotate(a, b);
return true;
}
I have another static library libSutil.a that contains "Arcball" class under namespace "sutil".
When I try the following test code:
#include "Camera.h"
int main() {
Camera cam ;
cam.rotate(1.0f, 2.9f);
}
I get the following link error:
./lib/libCam.a(Camera.cpp.o): In function
Camera::rotate(float, float):
/ray/irsa/nnn/src/IRay/Camera.cpp:118: undefined
reference to
sutil::Arcball::rotate(float2 const&, float2 const&)
const
collect2: error: ld returned 1 exit status
Looking at global mangled symbols in both libraries:
>nm -C ../../build/lib/libSutil.a | grep rotate
00000000000007cc T
sutil::Arcball::rotate(optix::float2 const&,
optix::float2 const&) const
>nm -C ../../build/lib/libCam.a | grep rotate
0000000000000020 b guard variable for
Camera::rotate(float, float)::arcball
00000000000044a6 T Camera::rotate(float, float)
U sutil::Arcball::rotate(float2 const&,
float2 const&) const
0000000000000010 b Camera::rotate(float,
float)::arcball
The question is why libCam.a symbol table dropped off optix:: namespace qualifier in rotate arguments. I even removed using name space, and qualified each variable explicity with optix:: prefix. But that did not make any difference. Please note that float2, Matrix4x4, make_float2 are all from "optix" name space.
Why is the linker failing?? I tried changing the library link order with no luck. What am I doing wrong.
After some more search I am now convinced the link failure is the artefact of CMAKE. My problem fits the following pattern. Class 'a' uses class 'b' main uses class 'a'.
CMAKE FILE PATTERN:
add_library(b STATIC b.cpp b.h)
add_library(a STATIC a.cpp a.h)
add_executable(main main.cpp)
I have tested this with two versions of CMAKE and the link fails. I think CMAKE is not designed to deal with this elegantly. The issue of missing name space qualifier is nothing to do with this. But that is another question to be answered.
Anyway now I can confirm the solution given here solved the linking issue.
target_link_libraries(c INTERFACE a b)
add_executable(main main.cpp)
target_link_libraries(main c)