namespace and undefined symbols on OSX

12 views
Skip to first unread message

Randy Heiland

unread,
Jul 11, 2019, 12:06:14 PM7/11/19
to sbml-interoperability
On OSX 10.14.5, I've installed libsbml-5.18.0-libxml2-macosx-mojave.dmg from
https://sourceforge.net/projects/sbml/files/libsbml/5.18.0/stable/Mac%20OS%20X/

and I'm using:
$ g++-9 --version
g++-9 (Homebrew GCC 9.1.0) 9.1.0


I would like to compile my code using the "libsbml" namespace, e.g.:

#include <iostream>
#include <sbml/SBMLTypes.h>
using namespace std;

int main (int argc, char* argv[])
{
libsbml::SBMLDocument* document = libsbml::readSBML(argv[1]);
cout << " error(s): " << document->getNumErrors() << endl;
}

However, I get:
$ g++-9 -DLIBSBML_USE_CPP_NAMESPACE -I/usr/local/include -std=c++11 -L/usr/local/lib -lsbml sbml_test.cpp
Undefined symbols for architecture x86_64:
"libsbml::SBMLDocument::getNumErrors() const", referenced from:
_main in cctircnZ.o
ld: symbol(s) not found for architecture x86_64

FYI:
$ ls -l /usr/local/lib/libsbml*
-rw-r--r-- 1 heiland staff 17352504 Apr 15 10:54 /usr/local/lib/libsbml-static.a
-rwxr-xr-x 1 heiland staff 7763352 Apr 15 10:54 /usr/local/lib/libsbml.5.18.0.dylib*
lrwxr-xr-x 1 heiland staff 20 Jun 23 20:03 /usr/local/lib/libsbml.5.dylib@ -> libsbml.5.18.0.dylib
lrwxr-xr-x 1 heiland staff 15 Jun 23 20:03 /usr/local/lib/libsbml.dylib@ -> libsbml.5.dylib

$ file /usr/local/lib/libsbml.5.18.0.dylib
/usr/local/lib/libsbml.5.18.0.dylib: Mach-O 64-bit dynamically linked shared library x86_64

------------------

If I remove the two "libsbml::" namespace prefixes on the first line (and remove the -D compile flag), it compiles/runs as expected:
$ g++-9 -I/usr/local/include -std=c++11 -L/usr/local/lib -lsbml sbml_test_no_ns.cpp

Can someone explain what I'm doing wrong? I've done a verbose compile on the former, but don't see anything obvious.

thanks, Randy

Frank T. Bergmann

unread,
Jul 12, 2019, 2:45:41 AM7/12/19
to sbml-interoperability
Hello, the issue is with adding 'LIBSBML_USE_CPP_NAMESPACE ' to the compile command line, rather then placing it into the source code file. This definition will expand to a using statement, if libSBML is compiled with C++ namespaces, or nothing otherwise. In your case it has to expand to 'using namespace libsbml', however you preclude that with your override on the command line. 

best
Frank

Randy Heiland

unread,
Jul 12, 2019, 6:32:07 AM7/12/19
to sbml-interoperability
Frank, thanks for the reply, but I'm still confused - sorry. Are you suggesting that I need to add:
#define LIBSBML_USE_CPP_NAMESPACE

into the top of my test program, rather than use the "-DLIBSBML_USE_CPP_NAMESPACE" on the compile command? If so, I did that, but get the same error (as I would have expected).

$ g++-9 -I/usr/local/include -march=native -fomit-frame-pointer -mfpmath=both -m64 -std=c++11 -L/usr/local/lib -lsbml sbml_test.cpp -o sbml_test
Undefined symbols for architecture x86_64:
"libsbml::SBMLDocument::getNumErrors() const", referenced from:
_main in ccTEEvPB.o
ld: symbol(s) not found for architecture x86_64

To be clear, I *want* to explicitly specify the "libsbml::" namespace on functions in my program.

-Randy

Frank T. Bergmann

unread,
Jul 12, 2019, 7:29:00 AM7/12/19
to sbml-interoperability
Sorry, no ... i meant to just include it verbatim in the source code the following will compile whether libSBML has been compiled with namespaces or without. the definition comes from (<sbml/common/libsbml-namespace.h>): 


"""
#include <iostream>
#include <sbml/SBMLTypes.h>
using namespace std; 

LIBSBML_USE_CPP_NAMESPACE


int main (int argc, char* argv[])
{
  libsbml::SBMLDocument* document = libsbml::readSBML(argv[1]);
  cout << "  error(s): " << document->getNumErrors()  << endl;
}

"""

Reading your initial email again, if you downloaded our precompiled binaries, this will not work for you, as the precopiled binaries do not use the c++ namespace. You will have to compile libSBML from source, setting the CMake option WITH_CPP_NAMESPACE=ON. 

best
Frank

Randy Heiland

unread,
Jul 12, 2019, 7:42:32 AM7/12/19
to sbml-interoperability
Ah, your last two sentences explain everything. 

thanks, Randy

Michael Hucka

unread,
Jul 12, 2019, 10:10:32 AM7/12/19
to sbml-interoperability
On 12 Jul 2019, at 4:29, Frank T. Bergmann wrote:
> Reading your initial email again, if you downloaded our precompiled
> binaries, this will not work for you, as the precopiled binaries do
> not use
> the c++ namespace. You will have to compile libSBML from source,
> setting
> the CMake option WITH_CPP_NAMESPACE=ON.

Where would be a good place to put a note about this? The download page
at http://sbml.org/Software/libSBML/Downloading_libSBML is already very
long, so I hesitate adding more text there.

MH
Reply all
Reply to author
Forward
0 new messages