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
micharaze
  • 957
  • 8
  • 25
  • Can you execute after building manually, i.e. with a handwritten commandline instead of via make? – Yunnosch Jan 10 '18 at 16:00
  • 3
    What does `file prog` tell you? – SergeyA Jan 10 '18 at 16:01
  • yes, i can build manually. – micharaze Jan 10 '18 at 16:03
  • @SergeyA do you mean the content? Starts with: ???x__TEXT??__text__TEXT?XS??__stubs__TEXT?c???__stub_helper__TEXTth?th?__gcc_except_tab__TEXT – micharaze Jan 10 '18 at 16:05
  • 2
    @raze92, no, I mean the exact command output – SergeyA Jan 10 '18 at 16:05
  • prog: Mach-O 64-bit bundle x86_64 – micharaze Jan 10 '18 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. – Basile Starynkevitch Jan 10 '18 at 16:07
  • 1
    Do you have "execute" permission flag set for your file? – Gerhardh Jan 10 '18 at 16:08
  • 2
    Reacting you your edit: you need specify Python runtime library to the linker as well. – SergeyA Jan 10 '18 at 16:21
  • @raze92, please do not update your question in such a way that it fundamentally changes it or adds a completely new dimension, as you did with your update 1. If you discover a new problem after the question you asked is answered, and if your research on that new problem is unavailing, then the right thing to do is to pose a new, separate question about the new problem. – John Bollinger Jan 10 '18 at 17:04

1 Answers1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157