python - 如何配置环境以使用 LLVM Clang 构建 python 嵌入式 c 代码

标签 python c macos clang python-3.3

我正在尝试编译一个用 c 编写的嵌入了 python 的程序。

我跑了python3.3-config -cflags获取我在此命令中使用的 cflags。

% >> clang -v einformer.c -o einformer - I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -disable-free -disable-llvm-verifier -main-file-name einformer.c -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 136 -v -g -resource-dir /usr/bin/../lib/clang/4.2 -D NDEBUG -I /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I /usr/local/include -I /usr/local/opt/sqlite/include -fmodule-cache-path /var/folders/lz/5nhsdlnn3x9g8ry_mkgtgh500000gp/T/clang-module-cache -c-isystem /usr/include/python2.6 -c-isystem . -O3 -Wno-unused-result -Wall -Wstrict-prototypes -fdebug-compilation-dir /Users/ardentapprentice/dev/osx/test -ferror-limit 19 -fmessage-length 140 -fwrapv -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.8.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fno-common -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/lz/5nhsdlnn3x9g8ry_mkgtgh500000gp/T/einformer-T6nymD.o -x c einformer.c
clang -cc1 version 4.2 based upon LLVM 3.2svn default target x86_64-apple-darwin12.2.0
ignoring duplicate directory "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m"
ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m
 /usr/local/opt/sqlite/include
 /usr/include/python2.6
 .
 /usr/local/include
 /usr/bin/../lib/clang/4.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
einformer.c:92:25: warning: implicit declaration of function 'PyString_AsString' is invalid in C99 [-Wimplicit-function-declaration]
    path_array[i] = PyString_AsString(pypath);
                    ^
1 warning generated.
 "/usr/bin/ld" -demangle -dynamic -arch x86_64 -dynamic -macosx_version_min 10.8.0 -o einformer /var/folders/lz/5nhsdlnn3x9g8ry_mkgtgh500000gp/T/einformer-T6nymD.o -lSystem /usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_PyList_GetItem", referenced from:
      _main in einformer-T6nymD.o
  "_PyList_Size", referenced from:
      _main in einformer-T6nymD.o
  "_PyRun_SimpleStringFlags", referenced from:
      _main in einformer-T6nymD.o
  "_PyString_AsString", referenced from:
      _main in einformer-T6nymD.o
  "_Py_Finalize", referenced from:
      _main in einformer-T6nymD.o
  "_Py_Initialize", referenced from:
      _main in einformer-T6nymD.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

clang 似乎在定位 header 时遇到问题? <Python.h>包含在源代码中我不确定我做错了什么:/

我花了大约 3 个小时浏览 python 文档 http://docs.python.org/3.3/c-api/index.html并四处搜索,但我似乎找不到任何对我有帮助的信息。

最佳答案

python3.3-config --cflags 仅提供查找 header 所需的参数。 python3.3-config --ldflags 将为您提供链接器标志。这意味着您应该能够使用类似以下的编译器调用来编译您的程序:

clang -v einformer.c -o einformer $(python3.3-config --cflags) $(python3.3-config --ldflags)

关于python - 如何配置环境以使用 LLVM Clang 构建 python 嵌入式 c 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15085413/

相关文章:

python - 广泛覆盖运算符(使用 "=="进行类型检查)

c++ - 如果我将 extern "C++"与 C 工具链一起使用会怎样?

c++ - "} while (0);"总是等于 "break;} while (1);"吗?

swift - 我们如何在 cocoa Mac OSX 应用程序中使用 NSWorkSpace 添加附件到默认邮件应用程序

python - 如何使用 Python SOAPpy 访问 Yahoo Enterprise Web Services?

python - 从 python 中成功的正则表达式匹配构建字典

c - 为什么 realloc 在重复调用时失败,而使用 malloc 分配大块是有效的?

python - 如何在 Mac 上修复 "cc: error: unrecognized command line option ' -Wshorten-64-to-3 2'"问题

macos - 错误安装openCV MacOS

python - 从 SQL Server 到 Postgres 的 '😕' 上的 SQLAlchemy UnicodeEncodeError