0

My cmake file

cmake_minimum_required(VERSION 3.13)
project(vd3)

set(CMAKE_CXX_STANDARD 11)
include_directories(/usr/local/include)

add_executable(vd3 main.cpp)

main.cpp

#include <iostream>
#include <sbml/SBMLTypes.h>

using namespace std;

int main (int argc, char* argv[])
{
    SBMLDocument* document = readSBML(argv[1]);
    unsigned int errors = document->getNumErrors();
    cout << endl;
    cout << "  filename: " << argv[1] << endl;
    cout << "  error(s): " << errors  << endl;
    cout << endl;
    if (errors > 0) document->printErrors(cerr);
    return errors;
}

Output

g++ -lm main.cpp -o main
Undefined symbols for architecture x86_64:
"SBMLDocument::printErrors(std::__1::basic_ostream<char, 
std::__1::char_traits<char> >&) const", referenced from:
  _main in main-98f75f.o
"SBMLDocument::getNumErrors() const", referenced from:
  _main in main-98f75f.o
"_readSBML", referenced from:
  _main in main-98f75f.o
ld: symbol(s) not found for architecture x86_64
Prakhar Agarwal
  • 2,724
  • 28
  • 31
  • You have to link against the `sbml` library. – t.niese Mar 12 '19 at 08:45
  • I have the sbml header files in the /usr/local/include directory. Isn't that sufficient? – Prakhar Agarwal Mar 12 '19 at 08:46
  • 1
    If sbml would be a header only library then it would be sufficient that all headers are found. A missing header would be a compiler error. But a `Undefined symbols for architecture` is a linking error, and as of that you need to link against that library. – t.niese Mar 12 '19 at 08:47
  • Dynamically-linked - libsbml.5.dylib Statically-linked - libsbml.a Libtool control file - libsbml.la I have these 3 files too. Which one do I need to use? And should I link it in the above cmake file? – Prakhar Agarwal Mar 12 '19 at 08:50
  • 1
    `target_link_libraries(vd3 /usr/local/lib/libsbml.5.dylib)` is the simplest thing that could work, but it is not very portable. – Botje Mar 12 '19 at 08:55
  • 1
    The easiest thing is to do static linking because you don't need to make sure that the dylib can be found when the application is launched. (But in your case it most certainly won't make a difference if you link against `.a` or `.dylib`) And you link your executable against the lib in your cmake file using `target_link_libraries`. – t.niese Mar 12 '19 at 08:55
  • @Botje: Tried, but it didn't work. – Prakhar Agarwal Mar 12 '19 at 09:03
  • @t.niese: This is what my cmake looks like now: cmake_minimum_required(VERSION 3.13) project(vd3) set(CMAKE_CXX_STANDARD 11) include_directories(/usr/local/include) target_link_libraries(vd3 /usr/local/lib/libsbmlsim-static.a) add_executable(vd3 main.cpp) This results in /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/studentuser/CLionProjects/vd3 CMake Error at CMakeLists.txt:7 (target_link_libraries): Cannot specify link libraries for target "vd3" which is not built by this project. – Prakhar Agarwal Mar 12 '19 at 09:05
  • 2
    You have to define the target first before you can `target_link_libraries` to it. – Botje Mar 12 '19 at 09:06
  • 1
    [Getting a CMake Error: Cannot specify link libraries for target which is not built by the project](https://stackoverflow.com/questions/25909943/getting-a-cmake-error-cannot-specify-link-libraries-for-target-which-is-not-bui/41262868) and [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – t.niese Mar 12 '19 at 09:08
  • @Botje Updated cmake: cmake_minimum_required(VERSION 3.13) project(vd3) set(CMAKE_CXX_STANDARD 11) include_directories(/usr/local/include) add_executable(vd3 main.cpp) target_link_libraries(vd3 /usr/local/lib/libsbmlsim-static.a) Same initial "Undefined symbols for architecture x86_64:" error – Prakhar Agarwal Mar 12 '19 at 09:08
  • This is sbml*sim*. Did you verify that this contains the proper symbols? `otool -tv` can tell you. – Botje Mar 12 '19 at 09:11
  • @Botje: Running otool -tv libsbmlsim-static.a printed long assembly code. Regarding verifying the package, I did remove and reinstall the library using the .dmg file – Prakhar Agarwal Mar 12 '19 at 09:17
  • Oh, duh. Wrong tool. `nm` should tell you what is inside the .a file. If it does not contain `readSBML` you are linking with the wrong library. – Botje Mar 12 '19 at 09:21
  • But why do you link against `libsbmlsim-static.a` and not against the `libsbml.a`? `libsbmlsim-static.a` belongs to [github: libsbmlsim/libsbmlsim](https://github.com/libsbmlsim/libsbmlsim), but the missing symbols are from `libsbml`. – t.niese Mar 12 '19 at 09:23
  • @t.niese i checked for all the files created in the /usr/local/lib directly, after running the dmg installer for the library(http://sbml.org/Software/libSBML/5.17.0/docs//cpp-api/libsbml-installation.html). These are the files it creates: libsbmlsim-static.a libsbmlsim.1.4.0.dylib libsbmlsim.1.4.dylib libsbmlsim.dylib – Prakhar Agarwal Mar 12 '19 at 09:24
  • 1
    Yes but before that you talked about `libsbml.5.dylib`, `libsbml.a` and `libsbml.la` then shown error is about `libsbml` and not about `libsbmlsim`. – t.niese Mar 12 '19 at 09:26
  • @t.niese: Yes, because that's what the documentation says(http://sbml.org/Software/libSBML/5.17.0/docs//cpp-api/libsbml-installation.html)- section 3. I am not sure what lead to this discrepency in file naming – Prakhar Agarwal Mar 12 '19 at 09:29
  • 1
    As I said `libsbmlsim.1.4.dylib` belongs to a different library, it belongs to libsbmlsim (version 1.4). You have to install libsbml, and link against that library. – t.niese Mar 12 '19 at 09:34
  • @t.niese Thanks, got it working! – Prakhar Agarwal Mar 12 '19 at 09:41

0 Answers0