【发布时间】:2018-01-10 15:57:58
【问题描述】:
当我在 make 之后运行可执行文件时,bash 会引发以下错误:
$ ./prog
-bash: ./prog: cannot execute binary file
我的生成文件:
CC = g++ -std=c++11
LDFLAGS = -undefined dynamic_lookup -bundle
OBJ = main.o datetime.o logger.o WeatherApi.o tinyxml2.o tflower.o tservo.o
prog: $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
%.o: %.cpp
$(CC) -c $<
clean:
rm -r *.o
我做错了什么?
更新 2:
感谢您的comment SergayA。在 this article 的帮助下链接 python 后,它可以工作了。
更新 1:
我在一些 cmets 之后删除了 -undefined dynamic_lookup -bundle 标志,所以我遇到了 Python 问题。要使用 Python.h,我将环境变量设置为此路径:
CPLUS_INCLUDE_PATH=/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/
2 warnings generated.
g++ -std=c++11 -c datetime.cpp
g++ -std=c++11 -c logger.cpp
g++ -std=c++11 -c WeatherApi.cpp
g++ -std=c++11 -c tinyxml2.cpp
g++ -std=c++11 -c tflower.cpp
g++ -std=c++11 -c tservo.cpp
g++ -std=c++11 -o prog main.o datetime.o logger.o WeatherApi.o tinyxml2.o tflower.o tservo.o
Undefined symbols for architecture x86_64:
"_PyDict_GetItemString", referenced from:
WeatherApi::SayHelloWorld() in WeatherApi.o
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyErr_Print", referenced from:
WeatherApi::WeatherApi(char*, char*) in WeatherApi.o
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyImport_Import", referenced from:
WeatherApi::WeatherApi(char*, char*) in WeatherApi.o
"_PyModule_GetDict", referenced from:
WeatherApi::WeatherApi(char*, char*) in WeatherApi.o
"_PyObject_CallObject", referenced from:
WeatherApi::SayHelloWorld() in WeatherApi.o
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyString_AsString", referenced from:
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyString_FromString", referenced from:
WeatherApi::WeatherApi(char*, char*) in WeatherApi.o
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyTuple_New", referenced from:
WeatherApi::GetAirTemperature() in WeatherApi.o
"_PyTuple_SetItem", referenced from:
WeatherApi::GetAirTemperature() in WeatherApi.o
"_Py_Finalize", referenced from:
WeatherApi::~WeatherApi() in WeatherApi.o
"_Py_Initialize", referenced from:
WeatherApi::WeatherApi(char*, char*) in WeatherApi.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: *** [prog] Error 1
【问题讨论】:
-
file prog告诉你什么? -
@raze92,不,我的意思是确切的命令输出
-
你应该知道,然后解释一下,你为什么使用
-undefined dynamic_lookup -bundle。所以编辑你的问题来改进它并添加更多关于你的Makefile的解释。如果您不知道为什么要使用它,则需要将其从Makefile中删除。大多数程序不需要这些额外的链接器标志。 -
您是否为您的文件设置了“执行”权限标志?
-
回应您的编辑:您还需要为链接器指定 Python 运行时库。
标签: python c++ macos makefile g++