I am learning OpenGL and GLUT recently. I programmed a simple program. This program runs very well in Visual Studio. But when I tried to use makefile to run it. It failed.
when I click the .exe file, it shows the following
In addition, My computer is Windows system. I run the program in my friend's computer, it works fine. His computer is MAC system.
My program has 3 files, paint.cpp, library.cpp, library.h
I hope someone can help me solve this. thanks
Here is my makefile
LDFLAGS = -lGL -lGLU -lglut
CFLAGS=-g -Wall -std=c++11
CC=g++
EXEEXT=
RM=rm
# Windows (cygwin)
ifeq "$(OS)" "Windows_NT"
EXEEXT=.exe #on windows applications must have .exe extension
RM=del #rm command for windows powershell
LDFLAGS = -lfreeglut -lglu32 -lopengl32
else
# OS X
OS := $(shell uname)
ifeq ($(OS), Darwin)
LDFLAGS = -framework Carbon -framework OpenGL -framework GLUT
endif
endif
PROGRAM_NAME= Paint
run: $(PROGRAM_NAME)
./$(PROGRAM_NAME)$(EXEEXT)
$(PROGRAM_NAME): paint.o library.o
$(CC) -o $# $^ $(CFLAGS) $(LDFLAGS)
clean:
$(RM) *.o $(PROGRAM_NAME)$(EXEEXT)
Related
So I am using nvidia's deepstream sdk and trying to modify the makefile of one of the sample examples given as I wish to link and add my own libraries. This is the makefile being employed where I am setting the path of the CUSTOM_LIB to point to the location of my library. The issue is the project gets compiled successfully but during run time, its unable to find the custom library. I performed ldd on the executable generated and there also it was showing the library as 'not found'. I think it's something to do with rpath but I am not sure about that.
APP:= sample
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
NVDS_VERSION:=4.0
LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/
ifeq ($(TARGET_DEVICE),aarch64)
CFLAGS:= -DPLATFORM_TEGRA
endif
CUDA_VER:=10.0
CC:=g++
SRCS:= $(wildcard ../src/*.c)
#SRCS+= $(wildcard ../../apps-common/src/*.c)
#SRCS+=
INCS:= $(wildcard ../include/*.h)
PKGS:= gstreamer-1.0 gstreamer-video-1.0 x11 opencv
OBJS:= $(SRCS:.c=.o)
CFLAGS+= -I../include -I/usr/include -I$(CUSTOM_LIB)/include -I/usr/local/cuda-10.0/targets/aarch64-linux/include/ -I/usr/include/jsoncpp -DDS_VERSION_MINOR=0 -DDS_VERSION_MAJOR=4 -fpermissive -Wnarrowing
LIBS+= -L$(LIB_INSTALL_DIR) -L/usr/lib/aarch64-linux-gnu -L$(CUSTOM_LIB)/lib -L/usr/lib/aarch64-linux-gnu/ -lcurl -letlic -letolm -lssl -lcrypto -llogger -lpthread -lsqlite3 -ljsoncpp -lnvdsgst_meta -lnvbufsurface -lnvbufsurftransform -lnvds_meta -lnvdsgst_helper -lnvds_utils -lm -L/usr/local/cuda-$(CUDA_VER)/lib64/ -lcudart \
-lgstrtspserver-1.0 -Wl,-rpath,$(LIB_INSTALL_DIR)
CFLAGS+= `pkg-config --cflags $(PKGS)`
LIBS+= `pkg-config --libs $(PKGS)`
all: $(APP)
debug: CXXFLAGS += -DDEBUG -g
debug: CFLAGS += -DDEBUG -g
debug: $(APP)
%.o: %.c $(INCS) Makefile
$(CC) -c -o $# $(CFLAGS) $<
$(APP): $(OBJS) Makefile
$(CC) -o $(APP) $(OBJS) $(LIBS)
clean:
rm -rf $(OBJS) $(APP)
You need to set rpath to a colon-separated list of directories where your libraries are found. You only add LIB_INSTALL_DIR but not CUSTOM_LIB_DIR. Generally everything you pass to -L you need to pass to -rpath too, unless there is a specific reason not to. For example, if you are building a package that has more than a single library and you are going to install in a standard place like /usr/lib, you don't have to add the directory where libraries temporarily live to -rpath. If you are going to install to a non-standard directory, add that directory.
I'm trying to create a makefile that will works on both OSX and Linux.
My problem is I need to change cflags and ldflags depending on OS the makefile is executed but I can't make it work. That's my makefile :
OS:=$(shell uname)
DST=hello
SRC=$(wildcard *.cpp)
OBJ=$(SRC:.cpp=.o)
CFLAGS=
all: clean DetectOS $(DST)
DetectOS:
ifeq ($(OS),Darwin)
#echo OS : $(OS)
CC=g++
LDFLAGS="-lm -framework OpenCL"
CFLAGS+=-O3
endif
ifeq ($(OS),Linux)
#Coming soon...
endif
$(DST): $(OBJ)
$(CC) -o $# $^ $(LDFLAGS)
%.o: %.cpp
$(CC) -o $# -c $< $(CFLAGS)
clean:
rm -rf *.o $(DST)
But when I run this code, neither cflags, ldflags or CC are updated in the ifeq conditional block. I get the following result :
$ make
rm -rf *.o hello
OS : Darwin
CC=g++
LDFLAGS="-lm -framework OpenCL"
CFLAGS+=-O3
cc -o opencl.o -c opencl.cpp
cc -o hello opencl.o
Undefined symbols for architecture x86_64:....
As you can see, the OS is detected because we went in the ifeq conditional block, but CC isn't updated and keep a non-initialized value of cc. Finally the linker process fails because OpenCL is not referenced in ldflags.
An other little point, if I don't put quotes in LDFLAGS="-lm -framework OpenCL" I get the error :
LDFLAGS=-lm -framework OpenCL
/bin/sh: -framework: command not found
make: *** [DetectOS] Error 127
And based on multiple exemples (here on stackoverflow) I should do it without quotes.
I'm currently on Mac OS X Yosemite.
I think the ifeq block should not go into the make target, but just to the front of the makefile (before all:).
I.e. like this:
OS:=$(shell uname)
DST=hello
SRC=$(wildcard *.cpp)
OBJ=$(SRC:.cpp=.o)
CFLAGS=
ifeq ($(OS),Darwin)
$(info OS is $(OS))
CC=g++
LDFLAGS=-lm -framework OpenCL
CFLAGS+=-O3
endif
ifeq ($(OS),Linux)
#Coming soon...
endif
all: clean $(DST)
...
(no "DetectOS" target, using info instead of echo)
I have an SDL game I have been working on as my first somewhat real project. I decided to introduce Box2D physics as I was not happy with the collision detection. So I installed it to /usr/local/lib/Box2D and in the folder is Box2D.h and supporting folders. I am using MacVim to code on OSX 10.9.2 to develop, and clang++ compiler from the command line.
In my game code I am just trying to create a simple world to test things out:
#include <Box2D/Box2D.h>
.......
world = new b2World(b2Vec2(0.0,9.81));
My make command finds the library, but errors out trying to build.
$ make clean && make
rm -rf obj bin
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Ball.cpp -o obj/Ball.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Game.cpp -o obj/Game.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Paddle.cpp -o obj/Paddle.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/TextureManager.cpp -o obj/TextureManager.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/main.cpp -o obj/main.o
clang++ -framework SDL2 -framework SDL2_image -F /Library/Frameworks -L/usr/local/lib/Box2D obj/Ball.o obj/Game.o obj/Paddle.o obj/TextureManager.o obj/main.o -o bin/game
Undefined symbols for architecture x86_64:
"b2World::b2World(b2Vec2 const&)", referenced from:
Game::init() in Game.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: *** [game] Error 1
And here is my Makefile. Box2D is in /usr/local/lib/Box2D/Box2D.h. I am pretty sure my issue is somewhere in the Makefile.
CXX = clang++
CXXFLAGS = -Wall -c -std=c++11 -I/usr/local/lib
SDL = -framework SDL2 -framework SDL2_image
LDFLAGS = $(SDL) -F /Library/Frameworks -L/usr/local/lib/Box2D
SRC_DIR = src
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
OBJ_DIR = obj
OBJECTS = $(subst $(SRC_DIR)/, $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(SOURCES)))
#$(warning $(OBJECTS))
BIN_DIR = bin
EXE = game
# run these no matter what
.PHONY: all clean run
all: $(EXE)
$(EXE): $(OBJECTS)
#mkdir -p $(BIN_DIR)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $(BIN_DIR)/$(EXE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
#mkdir -p $(OBJ_DIR)
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm -rf obj bin
run:
./$(BIN_DIR)/$(EXE)
It does not appear that you ever actually link the Box2D library anywhere? You use -L to specify directories to search while linking, but I don't see a -l option to actually link the Box2D library (whatever it's called).
Your output line seems to bear this out:
clang++ -framework SDL2 -framework SDL2_image -F /Library/Frameworks -L/usr/local/lib/Box2D obj/Ball.o obj/Game.o obj/Paddle.o obj/TextureManager.o obj/main.o -o bin/game
You need to get -lbox2d (or whatever the correct name for the Box2D library is) in there.
I seem to be going from one problem to the next ever since I decided to organize my code into subdirectories. The problems are naturally arising from the Makefile. So here's what I've currently got:
UNAME := $(shell uname)
# Directories
SOURCEDIR = src/
BUILDDIR = build/
# Compiler options
CC = clang++
DEBUG = -g
CFLAGS = -std=c++11 -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
# Files
SRC = $(wildcard $(SOURCEDIR)*.cpp) $(wildcard $(SOURCEDIR)**/*.cpp)
OBJS = $(SRC:$(SOURCEDIR)%.cpp=$(BUILDDIR)%.o)
ifeq ($(UNAME), Darwin)
LIBS = -lglfw3 -framework OpenGL -lglew -framework IOKit -framework CoreFoundation -framework ApplicationServices -framework Foundation -framework AppKit
BUILDDIR = ./build/osx/
endif
ifeq ($(UNAME), Linux)
LIBS = -lglfw -lGL -lGLEW
BUILDDIR = ./build/linux/
endif
# Build target
TARGET = test
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(LFLAGS) $? -o $(TARGET) $(LIBS)
$(OBJS): $(BUILDDIR)%.o : $(SOURCEDIR)%.cpp
#mkdir -p $(dir $#)
$(CC) $(CFLAGS) $< -o $#
clean:
rm -rf $(BUILDDIR)*.o $(BUILDDIR)**/*.o $(TARGET)
I was really glad when it actually compiled everything! Except when I made a change to a file, and tried to make it again, it spat this at me:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1
Thing is, when I make it once more, it works just fine. The problem seems to be with resolving dependencies? And for that, I need to specify a VPATH? Well, that's the closest I've gotten, except trying to specify a VPATH hasn't made a difference. I'm probably specifying it incorrectly, or then I'm taking the wrong approach to this.
I'm pretty inexperienced when it comes to Makefiles, so I'd really appreciate some guidance!
Thanks to Etan Reisner for the solution. The problem was with the difference between $? and $^. Here's the fixed version:
UNAME := $(shell uname)
# Directories
SOURCEDIR = src/
BUILDDIR = build/
# Compiler options
CC = clang++
DEBUG = -g
CFLAGS = -std=c++11 -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
# Files
SRC = $(wildcard $(SOURCEDIR)*.cpp) $(wildcard $(SOURCEDIR)*/*.cpp)
OBJS = $(SRC:$(SOURCEDIR)%.cpp=$(BUILDDIR)%.o)
ifeq ($(UNAME), Darwin)
LIBS = -lglfw3 -framework OpenGL -lglew -framework IOKit -framework CoreFoundation -framework ApplicationServices -framework Foundation -framework AppKit
BUILDDIR = ./build/osx/
endif
ifeq ($(UNAME), Linux)
LIBS = -lglfw -lGL -lGLEW
BUILDDIR = ./build/linux/
endif
# Build target
TARGET = test
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(LFLAGS) $^ -o $(TARGET) $(LIBS)
$(OBJS): $(BUILDDIR)%.o : $(SOURCEDIR)%.cpp
#mkdir -p $(dir $#)
$(CC) $(CFLAGS) $< -o $#
clean:
rm -rf $(BUILDDIR)*.o $(BUILDDIR)*/*.o $(TARGET)
I have been writing a plug-in for Maya with C++. The Makefile I use works fine on Snow Leopard, but does not in Lion. I am using the latest version of Maya 2012 in both cases. Here's the top of the error stack:
/Applications/Autodesk/maya2012/Maya.app/Contents/../../devkit/include/maya/OpenMayaMac.h:89:35: error:
AvailabilityMacros.h: No such file or directory
/Applications/Autodesk/maya2012/Maya.app/Contents/../../devkit/include/maya/OpenMayaMac.h:107:24: error:
sys/param.h: No such file or directory
/Applications/Autodesk/maya2012/Maya.app/Contents/../../devkit/include/maya/OpenMayaMac.h:114:40: error:
CoreServices/CoreServices.h: No such file or directory
This makes me think that these files are in a different location in Lion, but I do see them all in my /usr/include/ directory, just like Snow Leopard. Anyone have similar issues, ideas, suggestions?
The make file I use is below:
# NOTE: MAYA_LOCATION on Mac OS X points to Maya.app/Contents
MAYA_LOCATION = /Applications/Autodesk/maya2012/Maya.app/Contents
# Change location if a non standard install.
DEVKIT_LOCATION = $(MAYA_LOCATION)/../../devkit
C++ = g++
PREFERRED_ARCHITECTURE =
# Determine the architectures to build.
MAYABIN = ${MAYA_LOCATION}/bin/maya
MAYA_ARCHES = $(shell lipo -info $(MAYABIN) | sed 's/^.*://')
ifneq ($(PREFERRED_ARCHITECTURE),)
MAYA_ARCHES = $(filter $(PREFERRED_ARCHITECTURE),$(MAYA_ARCHES))
ifeq ($(MAYA_ARCHES),)
$(error $(MAYABIN) does not support the '$(PREFERRED_ARCHITECTURE)' architecture.)
endif
endif
ARCH_FLAGS = $(patsubst %,-arch %,$(MAYA_ARCHES))
CFLAGS = -DAW_NEW_IOSTREAMS -DCC_GNU_ -DOSMac_ -DOSMacOSX_ \
-DOSMac_MachO_ -DREQUIRE_IOSTREAM -fno-gnu-keywords -fpascal-strings -O3 \
$(ARCH_FLAGS) -D_LANGUAGE_C_PLUS_PLUS -isysroot /Developer/SDKs/MacOSX10.6.sdk \
-include $(MAYA_LOCATION)/../../devkit/include/maya/OpenMayaMac.h \
-shared
C++FLAGS = $(CFLAGS)
INCLUDES = -I. -I$(MAYA_LOCATION)/../../devkit/include
LDFLAGS = -framework Carbon -framework OpenGL -framework GLUT -lOpenMayaUI
LD = $(MAYA_LOCATION)/../../devkit/bin/mayald MAYA_ARCHES="$(MAYA_ARCHES)" MAYA_LOCATION="$(MAYA_LOCATION)"
all: VmExample.bundle
VmExampleNode.o: VmExampleNode.cpp
$(C++) -c VmExampleNode.cpp $(C++FLAGS) $(INCLUDES)
vmPluginMain.o: vmPluginMain.cpp
$(C++) -c vmPluginMain.cpp $(C++FLAGS) $(INCLUDES)
VmExample.bundle: VmExampleNode.o vmPluginMain.o
$(LD) -dynamic -bundle -o VmExample.bundle VmExampleNode.o vmPluginMain.o ../core/libVexample.o $(LDFLAGS)
same problem here; Managed to make it work by adding
-mmacosx-version-min=10.6
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk
to C++ Flags