mardi 8 janvier 2019

clang linker, ld: symbol(s) not found for architecture x86_64

I am trying to embedded some python code within C++ for a project. I have been able to run this simple tutorial on Windows and it worked (5.1 Very high Level embedding https://docs.python.org/2/extending/embedding.html)

but I wanted to implement it also on my personal Mac and got the following issue when building my project:

====================[ Build | TestCharacter | Debub ]===========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/michaelstettler/CLionProjects/TestCharacter/cmake-build-debug --target TestCharacter -- -j 4
Scanning dependencies of target TestCharacter
[ 50%] Building CXX object CMakeFiles/TestCharacter.dir/main.cpp.o
[100%] Linking CXX executable TestCharacter
Undefined symbols for architecture x86_64:
  "_PyMem_RawFree", referenced from:
      _main in main.cpp.o
  "_PyRun_SimpleStringFlags", referenced from:
      _main in main.cpp.o
  "_Py_DecodeLocale", referenced from:
      _main in main.cpp.o
  "_Py_FinalizeEx", referenced from:
      _main in main.cpp.o
  "_Py_Initialize", referenced from:
      _main in main.cpp.o
  "_Py_SetProgramName", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [TestCharacter] Error 1
make[2]: *** [CMakeFiles/TestCharacter.dir/all] Error 2
make[1]: *** [CMakeFiles/TestCharacter.dir/rule] Error 2
make: *** [TestCharacter] Error 2

My code is:

#include <iostream>
#include <string>
#include <Python.h> // modified the CMake to make it findable

using namespace std;

int main(int argc, char *argv[]) {
    cout << "Hello, World!" << endl;

    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                   "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

I had to modify my CmakeLists.txt to look for the headers as, at first, I was not able to find the Python.h library.

cmake_minimum_required(VERSION 3.13)
project(TestCharacter)

set(CMAKE_CXX_STANDARD 14)

include_directories(/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/Headers)

add_executable(TestCharacter main.cpp)

It seems that my linker is wrong but I haven't figure out what to do. I haven't been able to tool for the invocation either.

If it may be of any help, I am using Clion.

Aucun commentaire:

Enregistrer un commentaire