I'm trying to create an opengl project for this tutorial, and I can't get my program to compile.
The current problem here is that every glfw functions are undefined, but they exist in glfw3.h.
The glfw3 files are from the glfw download page (x64 version).
Here is a copy of the logs:
make
=== SRC ===
src/glad.c src/main.cpp
=== OBJ ===
bin/glad.o bin/main.o
===== Creating file bin/glad.o ===
C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/mkdir -p bin/
C:/MinGW/bin/g++ -I./include -o bin/glad.o -c src/glad.c
===== Creating file bin/main.o ===
C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/mkdir -p bin/
C:/MinGW/bin/g++ -I./include -o bin/main.o -c src/main.cpp
C:/MinGW/bin/g++ -L./libs -o build/program.exe bin/glad.o bin/main.o -lglfw3
bin/main.o:main.cpp:(.text+0xc): undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
make: *** [program.exe] Erreur 1
I'm using the following main.cpp file:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(int argc, char** argv){
glfwInit();
return 0;
}
And the following makefile:
CXX = C:/MinGW/bin/g++
FAKEPATH = C:/cygnus/cygwin-b20/H-i586-cygwin32/bin/
CXXFLAGS = -W -Wall -ansi -Wno-deprecated
LDLIBS = -lglfw3
TARGET = program.exe
LIB_DIR = -L./libs
INC_DIR = -I./include
SRC = $(shell $(FAKEPATH)find src/ -type f \( -iname \*.cpp -o -iname \*.c \))
OBJ = $(patsubst src/%,bin/%,$(addsuffix .o, $(basename $(SRC))))
all: directories $(TARGET)
test:
echo $(FAKEPATHEXISTS)
directories:
#$(FAKEPATH)echo === SRC ===
#$(FAKEPATH)echo $(SRC)
#$(FAKEPATH)echo === OBJ ===
#$(FAKEPATH)echo $(OBJ)
#$(FAKEPATH)mkdir -p bin
#$(FAKEPATH)mkdir -p build
compileAndRun: all
build/$(TARGET)
$(TARGET): $(OBJ)
$(CXX) $(LDFLAGS) $(LIB_DIR) -o build/$# $^ $(LDLIBS)
bin/%.o: src/%.c
#$(FAKEPATH)echo ===== Creating file $# ===
$(FAKEPATH)mkdir -p $(dir $#)
$(CXX) $(INC_DIR) -o $# -c $<
bin/%.o: src/%.cpp
#$(FAKEPATH)echo ===== Creating file $# ===
$(FAKEPATH)mkdir -p $(dir $#)
$(CXX) $(INC_DIR) -o $# -c $<
clean:
#$(FAKEPATH)rm -rf bin/
cleaner: clean
#$(FAKEPATH)rm -rf build/
And finally, my files are sorted like this:
I'm using MinGW for g++ and cygwin for the linux commands.
Any help or try would be really appreciated ! (I really don't know where the problem came from)
Thanks !
Note: I already searched on google a lot without finding any working solution (including several stack overflow questions)
I found a solution to this.
The first things I had to do is add #define GLFW_DLL in my main.cpp.
After doing this I had the error Undefined reference to '_imp__glfwInit'. To solve this error I had to remove the *.a files in my /libs, and then I moved my .dll file to the root of the project (To avoid a missing dll error).
Related
I have trouble with include uaplatformlayer.h in example from OPCUA client example.
I found this example in SDK. I tried to do own makefile, to build example client lesson01.
I use Visual Studio Code. It can't find this .h file.
#include "uaplatformlayer.h"
#include "sampleclient.h"
int main(int, char*[])
{
UaStatus status;
// Initialize the UA Stack platform layer
UaPlatformLayer::init();
// Create instance of SampleClient
SampleClient* pMyClient = new SampleClient();
return 0;
}
it still replies...
g++ -g -Wall -c client_cpp_sdk_tutorial.cpp
client_cpp_sdk_tutorial.cpp:1:10: fatal error: uaplatformlayer.h: Adresář nebo soubor neexistuje
1 | #include "uaplatformlayer.h"
| ^~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:9: client_cpp_sdk_tutorial.o] Chyba 1
The terminal process "bash '-c', '/home/michal/Dokumenty/OPCUA_adapter/sdk/examples/client_gettingstarted/lesson01/build.sh'" failed to launch (exit code: 2)
I think my problem is in makefile, but I don't have any idea where is the mistake.
Could anyone help me, please?
Does anyone experiance with OPCUA SDK?
cc=g++
cflags=-g -Wall
libflags=-L/home/michal/Dokumenty/OPCUA_adapter/sdk/lib -luamoduled -luamodelsd -lcoremoduled -luabasecppd -luastackd -lxmlparsercppd -luapkicppd -luaclientcppd -lxml2 -lssl -lcrypto
includes=-I/home/michal/Dokumenty/OPCUA_adapter/sdk/include/uabasecpp -I/home/michal/Dokumenty/OPCUA_adapter/sdk/include/uastack
objfiles=client_cpp_sdk_tutorial.o sampleclient.o
vystup=aplikace
%.o : %.cpp
$(cc) $(cflags) -c $<
# startovaci pravidlo
vychozi: $(vystup)
# zavislosti
dep:
$(cc) -MM *.cpp >dep.list
-include dep.list
clean:
rm aplikace $(objfiles)
# slinkování aplikace
$(vystup): $(objfiles)
$(cc) $(cflags) $(objfiles) $(includes) $(libflags) -o $#
You need to add "includes" to the recipe for the .o files:
%.o : %.cpp
$(cc) $(cflags) $(includes) -c $<
I am trying to write a make file for the following program
MY file/folder structure is as follows
Folder/File structure
./demo/utilities.c
./demo/utilities.h
./demo/makefile
./include/GeographicLib/UTMUPS.h
./include/GeographicLib/Constant.h
./include/GeographicLib/xxxxxxx
in the file utilities.h
#include <GeographicLib/UTMUPS.h>
in the file UTMUPS.h
#include <GeographicLib/Constant.h>
in the makefile
# preprocessor
PREPROC_FLAGS += -DEIGEN_DONT_ALIGN_STATICALLY
INC_XTRA_DIR = ../include
CC=g++
CPPFLAGS= $(PREPROC_FLAGS)
CFLAGS=-O2 -g -Wall -W -I$(INC_XTRA_DIR)
CXXFLAGS=-O2 -g -Wall -W -fpic -std=c++11
# short hand
OBJDIR=obj
Utilities_h = utilities.h GeographicLib/UTMUPS.hpp
utilities.o: utilities.c $(Utilities_h)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
all: $(FINAL_TARGET)
$(FINAL_TARGET): $(OBJ)
$(CC) -g -o $# $^ $(LDFLAGS)
ifneq ($(wildcard ../databases/.),)
cp $# ../
endif
TargetList = $(FINAL_TARGET)
clean:
rm -f $(TargetList) *.o *~
echo Clean done.
The question I want to ask is
When I compile the following project, it say it can't find "#include 'GeographicLib/UTMUPS.h'". in the utilities.h. what should the naming be in this case. My thought is that by adding -I$(INC_XTRA_DIR), or ../include ... it should search for GeographicLib/UTMUPS.h
what about the file that UTMUPS.h is dependent on(in this case Constant.h), what should be the addressing
Edit: I run make at the root directory... maybe that's the reason for the error.
THanks
I have a project that I want to build a shared library for it. The following Makefile works:
libfastpd.so: fastpd.cpp
$(CXX) -std=c++11 -fPIC -c fastpd.cpp -o fastpd.o
$(CXX) -std=c++11 -fPIC -c graph.cpp -o graph.o
$(CXX) -std=c++11 -fPIC -c LinkedBlockList.cpp -o LinkedBlockList.o
$(CXX) -std=c++11 -fPIC -c maxflow.cpp -o maxflow.o
$(CXX) -std=c++11 -shared -Wl,-soname,libfastpd.so -o libfastpd.so fastpd.o graph.o LinkedBlockList.o maxflow.o
clean:
rm *.o *.so
Then I came across this recipe in Cogswell et al.'s C++ Cookbook: https://www.oreilly.com/library/view/c-cookbook/0596007612/ch01s18.html
and decided to improve my Makefile based on that:
# Specify extensions of files to delete when cleaning
CLEANEXTS = o so
# Specify the source files, the target files,
# and the install directory
SOURCES = fastpd.cpp graph.cpp LinkedBlockList.cpp maxflow.cpp
OUTPUTFILE = libfastpd.so
INSTALLDIR = ./
.PHONY: all
all: $(OUTPUTFILE)
# Build lib*.so from all the *.o;
# subst is the search-and-replace
# function demonstrated in Recipe 1.16
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES))
$(CXX) -shared -fPIC $(LDFLAGS) -o $# $^
.PHONY: install
install:
mkdir -p $(INSTALLDIR)
cp -p $(OUTPUTFILE) $(INSTALLDIR)
.PHONY: clean
clean:
for file in $(CLEANEXTS); do rm -f *.$$file; done
# Generate dependencies of .ccp files on .hpp files
include $(subst .cpp,.d,$(SOURCES))
%.d: %.cpp
$(CC) -M $(CPPFLAGS) $< > $#.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $# : ,g' < $#.$$$$ > $#; \
rm -f $#.$$$$
Running this file I obtained the following error:
/usr/bin/ld: fastpd.o: relocation R_X86_64_32 against `.rodata' can
not be used when making a shared object; recompile with -fPIC
fastpd.o: error adding symbols: Bad value
Checking the terminal output, I observed that the following commands were executed:
g++ -c -o fastpd.o fastpd.cpp
g++ -c -o graph.o graph.cpp
g++ -c -o LinkedBlockList.o LinkedBlockList.cpp
g++ -c -o maxflow.o maxflow.cpp
No -fPIC!
My question is: Which lines of the Makefile execute these commands and how to add -fPIC to them?
Any references to good ressources to understand the entire Makefile above would be very much appreciated as well!
Thank you very much in advance for your help!
Which lines of the Makefile execute these commands... ?
The short answer is none. The rule...
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES))
$(CXX) -shared -fPIC $(LDFLAGS) -o $# $^
only specifies the link time dependencies and command. The -fPIC option needs to be specified when you compile the source file but you haven't provided any rule to build a .o from a .cpp so make falls back on its implicit rule which (for the purposes of this example) is essentially...
%.o: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
So the obvious solution is to add -fPIC to CXXFLAGS...
CXXFLAGS += -fPIC
My Makefile:
CXX = clang++
CXXFLAGS = -g -Wall -std=c++14
LDFLAGS = -lboost_system -lcrypto -lssl -lcpprest -lpthread
OBJDIR = obj
SRCDIR = .
SRC := $(shell find $(SRCDIR) -name "*.cpp")
OBJ := $(SRC:%.cpp=%.o)
APP = run
all: $(APP)
$(APP): $(OBJ)
#echo "== LINKING EXECUTABLE $(APP)"
#$(CXX) $^ $(LDFLAGS) -o $(APP)
%.o: %.cpp
#echo "COMPILING SOURCE $< INTO OBJECT $#"
#$(CXX) -c $(CXXFLAGS) $< -o $#
clean:
find . -name *.o -delete
rm -f $(APP)
Directory structure:
Makefile
sources/
directory1
...cpp
directory2
...cpp
...
main.cpp
obj/
I try to make make create *.o files in a directory obj/ and then compile the final executable from there. I tried various approaches and they fail because of the project structure that stores *.cpp files in sub-directories. Particularly, I've tried the following: https://stackoverflow.com/a/26032630/2042546
I've also tried to manipulate the command itself clang++ $< -o obj/$# but it breaks whole idea of make and it's dependency management.
If I modify OBJ via patsubstr and notdir, make becomes unable to deduce dependency of a *.o on a corresponding *.cpp by it's path, cause *.o's path loses it's directory part and becomes unable to find it's *.cpp file when executing %.o:%.cpp rule (I hope I managed to write down my thoughts correctly).
If you want objects to live in the same source directory structure but under obj, then simply change your pattern rule (and how you generate the object files). And you should create the directory first:
OBJ := $(SRC:%.cpp=$(OBJDIR)/%.o)
...
$(OBJDIR)/%.o: %.cpp
#echo "COMPILING SOURCE $< INTO OBJECT $#"
#mkdir -p '$(#D)'
#$(CXX) -c $(CXXFLAGS) $< -o $#
--folder
----book.h
----book.cpp
----library.h
----library.cpp
----main.cpp
this is the directory structure and I have created this make file which generate object files and executable files in the same directory.
main : main.o book.o library.o
g++ main.o book.o library.o -o main
main.o : main.cpp book.h library.h
g++ -c main.cpp
library.o : library.cpp library.h book.h
g++ -c library.cpp
book.o : book.cpp book.h
g++ -c book.cpp
clean:
rm -f *.o main
But i want to create folders obj and bin respectively for object files and executable files in the folder. How can I do it?
Maybe something like this
TARGET = bin/main
SOURCES = main.cpp book.cpp library.cpp
OBJ_DIR = obj
OBJECTS = $(SOURCES:%.cpp=$(OBJ_DIR)/*.o)
$(TARGET): $(OBJECTS)
$(LD) $(LDFLAGS) $^ -o $# $(LIBS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) $< -c -o $#
This requires that the bin and obj directories exist first.
Read the GNU Make manual for more information.
I had some time over so I wrote up a complete and automated Makefile for you. This will create the directories if they do not exist.
BINDIR = bin/
OBJDIR = obj/
TARGET = $(BINDIR)target
SOURCES = main.cpp book.cpp library.cpp
LD = g++
OBJECTS = $(SOURCES:%.cpp=$(OBJDIR)%.o)
.PHONY: all
all: target
.PHONY: target
target: before_target actual_target after_target
.PHONY: before_target
before_target: $(OBJDIR) $(BINDIR)
.PHONY: actual_target
actual_target: $(TARGET)
.PHONY: after_target
after_target:
$(TARGET): $(OBJECTS)
$(LD) $(LDFLAGS) $^ -o $# $(LIBS)
$(OBJDIR)%.o: %.cpp
$(CXX) $(CXXFLAGS) $^ -c -o $#
$(OBJDIR) $(BINDIR):
mkdir $#
.PHONY: clean
clean:
-rm -rf $(BINDIR)
-rm -rf $(OBJDIR)
Use VPATH! Here is an article that does an excellent (and detailed) job of showing you how: Multi-Architecture Builds.
And here is the basic theory:
First, there will be a single level of recursion. This will be to handle automatically cding into the object directory, not for any directory-hierarchy reason.
Second, you will use VPATH to specify the location of the source code relative to the object directory: VPATH=../src
Third ... um ... profit!
Here's a simplified example (not tested). You can expand this, or follow along with the article to get a "richer" version:
ifneq (obj,$(notdir $(CURDIR)))
# Not in object directory, so we must be in root (src) directory!
.SUFFIXES:
OBJDIR := obj
MAKETARGET = $(MAKE) --no-print-directory -C $# -f $(CURDIR)/Makefile \
SRCDIR=$(CURDIR) $(MAKECMDGOALS)
.PHONY: $(OBJDIR)
$(OBJDIR):
;+#[ -d $# ] || mkdir -p $#
+#$(MAKETARGET)
Makefile : ;
%.mk :: ;
% :: $(OBJDIR) ; :
.PHONY: clean
clean:
rm -rf $(OBJDIR)
else
VPATH=../src
CXX := g++ # The default, just so you know it's here
# CXXFLAGS = ...
# LDFLAGS = ...
# LDLIBS = ...
main : main.o book.o library.o
main.o : main.cpp book.h library.h
library.o : library.cpp library.h book.h
book.o : book.cpp book.h
endif
You can just prefix your objects with obj/ and your executable with bin/ and add a command to make the directories mkdir -p bin etc..
# make your subfolders targets
all: bin obj bin/main
bin/main : obj/main.o obj/book.o obj/library.o
g++ -o bin/main obj/main.o obj/book.o obj/library.o
obj/main.o : main.cpp book.h library.h
g++ -c -o obj/main.o main.cpp
obj/library.o : library.cpp library.h book.h
g++ -c -o obj/library.o library.cpp
obj/book.o : book.cpp book.h
g++ -c -o obj/book.o book.cpp
# create subfolders if not present
bin obj:
mkdir -p bin
mkdir -p obj
clean:
rm -f bin/* obj/*
Then just type make (makes targer all).