# XCode2 - C++ link errors



## DustinM (Sep 27, 2005)

I am having difficulty getting a simple C++ test linking in XCode2.  I created a sample test where I have a Dynamic library that uses a class contained within a Static library.  This results in an "undefined symbols" link error.  Relevant code:


```
[Target: JNITest (dynamic library)]
--FILE: JNITest.cpp

#include "BSDSample.h"

int shared_function(const char *arg)
{
    BSDTest test( 5 );
    printf( "[BSDTest] Local: %d Static: %d", test.getLocal(), test.getStatic() );
    return 0;
}

[Target: BSDSample (static library)]
--FILE: BSDSample.h

class __attribute__((visibility("default"))) BSDTest
{
private:
	static int sm_nStatic;
	int m_nLocal;
public:
	BSDTest(int nNum) : m_nLocal( nNum ) {}
	int getLocal() const;
	int getStatic() const;
};

--FILE: BSDSample.cpp

#include "BSDSample.h"

int BSDTest::sm_nStatic = 10;
int BSDTest::getLocal() const {	return m_nLocal; }
int BSDTest::getStatic() const { return sm_nStatic; }
```

The target JNITest has as a dependency target: BSDSample

The link error I get is:
    Undefined symbols:
      __ZNK7BSDTest8getLocalEv
      __ZNK7BSDTest9getStaticEv
    internal link edit command failed

In the terminal, I verified that these are exported via:
  nm libBSDSample.a

which returns:
  libBSDSample.a(BSDSample.o):
  00000024 D __ZN7BSDTest10sm_nStaticE
  00000000 T __ZNK7BSDTest8getLocalEv
  00000000 A __ZNK7BSDTest8getLocalEv.eh
  00000008 T __ZNK7BSDTest9getStaticEv
  00000000 A __ZNK7BSDTest9getStaticEv.eh

Any ideas on how to get this to link?

Thanks!
Dustin


----------



## Viro (Sep 28, 2005)

I am not familiar with all those non-standard C++ directives in your source code. Perhaps if you what they were meant to do myself and others may be able to help.


----------



## DustinM (Sep 28, 2005)

Viro,

I am guessing what you meant by 





> all those non-standard C++ directives in your source code


 is the usage of "__attribute__((visibility("default")))" in the class declaration.  According to the GCC docs, this is GCC's equivalent to Microsoft VC++ .NET's "__declspec(dllexport)".  I am using the "__attribute__((visibility("default")))" class decorator in GCC v4.02 on GNU/Linux which works as expected.  I get the same results if I remove the "__attribute__((visibility("default")))" and just have:


```
class BSDTest
{
private:
	static int sm_nStatic;
	int m_nLocal;
public:
	BSDTest(int nNum) : m_nLocal( nNum ) {}
	int getLocal() const;
	int getStatic() const;
};
```
Are you able to get any Dynamic Library to properly link with any Static Library (that you build in the same project, not a Framework) in XCode2?  If I can get a working sample project even, I can analyze it to see how it works.  This seems like it should be an easy task (indeed it is in MS VC++ .NET and GCC on GNU/Linux)...  Actually, this code properly links using GCC from Eclipse on MacOS X, but I need to use the Cocoa Framework eventually as well (which Eclipse's CDT doesn't seem to recognize the Objective-C in the headers).  ...I am having a hell of a time in XCode2.  

Any help would be greatly appreciated...  I feel like I am spinning my wheels at this point.  

Thanks!
Dustin


----------



## DustinM (Sep 30, 2005)

I have resolved this issue...  by switching to Eclipse with CDT, which worked like a charm.  I am now able to compile/link the example posted here (along with my main code-base) without problems now.  Still not sure why I couldn't get XCode2 to work in such a simple case...

Dustin


----------



## Viro (Sep 30, 2005)

Sorry for not looking at this thread sooner. Work and all that .

Let me try to understand what you're doing, before I attempt it myself. You're trying to build a static library, that uses code in a dynamic library? That shouldn't be too hard as I've done something similar with my own library and the GNU Scientific Library (GSL).


----------

