1

When i run the executable after make, bash raise following error:

$ ./prog 
-bash: ./prog: cannot execute binary file

My makefile:

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

What did i wrong?


Update 2:

Thanks for your comment SergayA. After link python with the help of this article it works.


Update 1:

I removed the -undefined dynamic_lookup -bundle flag after some comments so i got problems with Python. To use Python.h i set the environment variable to this path: 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
10
  • 3
    What does file prog tell you? Commented Jan 10, 2018 at 16:01
  • 2
    @raze92, no, I mean the exact command output Commented Jan 10, 2018 at 16:05
  • 1
    You should know, then explain, why do you use -undefined dynamic_lookup -bundle. So edit your question to improve it and add more explanation about your Makefile. If you don't know why you use that you need to remove it from your Makefile. Most programs don't need such extra linker flags. Commented Jan 10, 2018 at 16:07
  • 1
    Do you have "execute" permission flag set for your file? Commented Jan 10, 2018 at 16:08
  • 2
    Reacting you your edit: you need specify Python runtime library to the linker as well. Commented Jan 10, 2018 at 16:21

1 Answer 1

6

What did i wrong?

Since you apparently wanted a program that you can execute directly, what you did wrong was to specify the -bundle option to g++. This instructs it that the output should be a Mach-o bundle format file. Such files are not directly executable. They can be loaded into another program at runtime via the system's dynamic linking functions, which can be used to implement plugins and similar program features. Remove this option.

The -undefined dynamic_lookup looks suspicious, too. I'm guessing that you copied that and the -bundle option from some other Makefile. If you don't know what it's there for then you ought to drop it, too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.