For context, I'm trying to compile source to a 32-bit executable for Windows using a Linux machine. I'm using the current mingw-w64 via apt-get. Here's the project I'm trying to compile ftp://ftp.thegpm.org/projects/tandem/source. More specifically the 17-02-01 zip files contain the source I'm interested in.
My first attempt was to just edit with the Makefile_ubuntu under the tandem-linux and swap out the gcc with the one provided by mingw and fix header reference issues that cropped up by adding #includes to .cpp files that threw errors. Super hacky. Can someone show me a brighter path?
Here's the makefile I'm using:
#makefile for c++ programs
#change the name of the executable to use for "any" project
EXECUTABLE = ../bin/tandem.exe
#EXECUTABLE = ../bin/p3.exe
LINKCC = $(CXX)
#CXXFLAGS denotes flags for the C++ compiler
CXX = /usr/bin/i686-w64-mingw32-g++-win32
#uncomment this line if you are using gcc 4.x
CXXFLAGS = -m32 -std=gnu++11
#CXXFLAGS = -w -O2 -DGCC4_3
#CXXFLAGS = -w -O2 -DGCC4_3 -DX_P3
#ubuntu 64 bit version
#LDFLAGS = -L/usr/lib/x86_64-linux-gnu/libexpat.a
LDFLAGS = -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a
#LDFLAGS = -lpthread -L/usr/lib -lm -lexpat
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
DEPS := $(patsubst %.o,%.d,$(OBJS))
all: $(EXECUTABLE)
#define the components of the program, and how to link them
#these components are defined as dependencies; that is they must be up-to-date before the code is linked
$(EXECUTABLE): $(DEPS) $(OBJS)
$(LINKCC) $(CXXFLAGS) -o $(EXECUTABLE) $(OBJS) $(LDFLAGS)
#specify the dep files depend on the cpp files
%.d: %.cpp
$(CXX) -M $(CXXFLAGS) $< > $#
$(CXX) -M $(CXXFLAGS) $< | sed s/\\.o/.d/ > $#
clean:
-rm $(OBJS) $(EXECUTABLE) $(DEPS) *~
explain:
#echo "The following info represents the program:"
#echo "Final exec name: $(EXECUTABLE)"
#echo "Source files: $(SRCS)"
#echo "Object files: $(OBJS)"
#echo "Dep files: $(DEPS)"
depend: $(DEPS)
#echo "Deps are now up-to-date."
-include $(DEPS)
And here is the error(s):
sudo make -f Makefile_ubuntu
/usr/bin/i686-w64-mingw32-g++-win32 -m32 -std=gnu++11 -o ../bin/tandem.exe tandem.o p3mprocess.o saxmzdatahandler.o mspectrumcondition.o masscalc.o mprocess.o mreport.o mscore_tandem.o loadmspectrum.o mplugin.o msequenceserver.o saxtaxhandler.o msequencecollection.o mscore.o mrefine.o xmltaxonomy.o mbiomlreport.o saxtandeminputhandler.o saxhandler.o msequtilities.o base64.o saxmodhandler.o mtermmods.o xmlparameter.o saxsaphandler.o saxmzxmlhandler.o saxmzmlhandler.o mxxcleavage.o p3msequenceserver.o mzid_report.o saxbiomlhandler.o p3.o mpmods.o saxgamlhandler.o stdafx.o MSNumpress.o mpam.o -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a
saxhandler.o:saxhandler.cpp:(.text+0x100): undefined reference to `XML_ParserCreate'
saxhandler.o:saxhandler.cpp:(.text+0x11d): undefined reference to `XML_SetUserData'
saxhandler.o:saxhandler.cpp:(.text+0x13b): undefined reference to `XML_SetElementHandler'
saxhandler.o:saxhandler.cpp:(.text+0x151): undefined reference to `XML_SetCharacterDataHandler'
saxhandler.o:saxhandler.cpp:(.text+0x1cf): undefined reference to `XML_ParserFree'
saxhandler.o:saxhandler.cpp:(.text+0x344): undefined reference to `XML_Parse'
saxhandler.o:saxhandler.cpp:(.text+0x37f): undefined reference to `XML_Parse'
saxhandler.o:saxhandler.cpp:(.text+0x3bd): undefined reference to `XML_GetErrorCode'
saxhandler.o:saxhandler.cpp:(.text+0x3d4): undefined reference to `XML_GetCurrentLineNumber'
collect2: error: ld returned 1 exit status
Makefile_ubuntu:33: recipe for target '../bin/tandem.exe' failed
make: *** [../bin/tandem.exe] Error 1
You are (incorrectly) linking with /usr/lib/x86_64-linux-gnu/libexpat.a which is a Linux library (in ELF format). You need to get some Windows version of it.
BTW -L/usr/lib/x86_64-linux-gnu/libexpat.a is incorrect. Since -L should give a directory not a library to link
At last, recent versions of Windows might have WSL which could be useful to you (you'll compile a Linux, mostly statically linked, executable, and it might run on the command line on Windows).
Related
I can compile the project just fine if I run the project by hand with g++ source/* -lSDL2 -o bin/fly_fishing. When I do run make, I get
mkdir -p bin
g++ -lSDL2 -o bin/fly_fishing
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x17): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:20: bin/fly_fishing] Error 1
Which tells me that it's not populating from $^ for linking. So what have I missed here? Here's the makefile for reference.
SRC_DIR := source
OBJ_DIR := objects
BIN_DIR := bin
EXE := $(BIN_DIR)/fly_fishing
SRC := $(wildcard $(SRC_DIR)/*.c)
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CXXFLAGS := -Wall
#CFLAGS := -Wall
LDLIBS := -lSDL2a
LDFLAGS :=
.PHONY: all clean
all: $(EXE)
$(EXE): $(OBJ) | $(BIN_DIR)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $#
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CXX) -c $(CXXFLAGS) $(CFLAGS) $< -o $#
$(BIN_DIR) $(OBJ_DIR):
mkdir -p $#
clean:
#$(RM) -rv $(BIN_DIR) $(OBJ_DIR)
-include $(OBJ:.o=.d)
Which tells me that it's not populating from $^ for linking.
That seems unlikely. Much more likely would be that $^ expands to nothing. Which would be the case if $(OBJ) expands to nothing. Which seems plausible because I don't see any corresponding objects being built (though perhaps you've omitted that, or they were built on a previous run). And $(OBJ) expanding to nothing implies that $(SRC) expands to nothing.
So what have I missed here?
That $(SRC) expands to nothing is not inconsistent with the data presented. I observe that the manual compilation command you present is
g++ source/* -lSDL2 -o bin/fly_fishing
That does seem to suggest that there are indeed source files in source/, but do they match the pattern source/*.c? Since you're compiling with g++, I bet not. It would be highly unconventional to name C++ source files to end with .c, and surely you would not attempt to compile C source files with a C++ compiler. I infer, then, that your source files are all named with .cpp, or maybe .cc or .C, all of which forms are conventions for C++ source names.
If all your source names follow one or another of those patterns then indeed, this ...
SRC := $(wildcard $(SRC_DIR)/*.c)
... will result in $(SRC) being empty.
this is my makefile:
### OUTPUT ###
BUILD = Build
### SOURCES ###
SRC = Src/main\
Src/Game/Game\
Src/Shader/Shader
### OBJECTS ###
OBJ = $(addsuffix .o, $(addprefix $(BUILD)/, $(SRC)))
### INCLUDES ###
INC = -ID:/Development/OpenGL/Glfw/include/GLFW\
-ID:/Development/OpenGL/Glew/glew-2.1.0/include/GL\
-ID:/Development/OpenGL/Glm/glm-0.9.9.8/glm\
-ISrc/Game\
-ISrc/Shader/
### LIBRARIES ###
LIB = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s\
-LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3\
-lopengl32
### LINKER FLAGS ###
LFLAGS = $(LIB)
### COMPILER FLAGS
CFLAGS = $(INC)
### COMPILER ###
CC = g++
all: $(BUILD)/test.exe
$(BUILD)/test.exe: $(OBJ)
#echo LINKING $^
#$(CC) $(LFLAGS) -o $# $^
$(BUILD)/%.o: %.cpp
#echo COMPILING $<
#mkdir -p $(subst /,\,$(dir $#))
#$(CC) $(CFLAGS) -M -MT $# -o $(patsubst %.o, %.d, $#) $<
#$(CC) $(CFLAGS) -o $# -c $<
-include $(OBJ:.o=.d)
.PHONY: clean
clean:
#echo CLEANING......
##rm -rf $(BUILD)/*
i am specifying the needed libraries here:
### LIBRARIES ###
LIB = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s\
-LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3\
-lopengl32
and linking them here:
$(BUILD)/test.exe: $(OBJ)
#echo LINKING $^
#$(CC) $(LFLAGS) -o $# $^
the errors i am getting are:
LINKING Build/Src/main.o Build/Src/Game/Game.o Build/Src/Shader/Shader.o
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xb): undefined reference to `__glewUseProgram'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x33): undefined reference to `__glewCreateShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x48): undefined reference to `__glewShaderSource'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x70): undefined reference to `__glewCompileShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xdf): undefined reference to `__glewCreateShader'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0xf4): undefined reference to `__glewShaderSource'
Build/Src/Shader/Shader.o:Shader.cpp:(.text+0x11c): undefined reference to `__glewCompileShader'
also i have defined GLEW_STATIC:
#define GLEW_STATIC
#include "glew.h"
i am using mingw32 and i'm calling the makefile with mingw32-make all
I have no idea why it can't find those symbols...i've been trying to figure it out for 2 hours..
EDIT:
Here is the actual linking command:
g++ -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 -lglew32s -LD:/Development/OpenGL/Glfw/lib-mingw -lglfw3 -lopengl32 -o Build/test.exe Build/Src/main.o Build/Src/Game/Game.o Build/Src/Shader/Shader.o
In a single-pass linker (like almost all linkers today) the order of items is critical. In particular, you have to make sure that all your object files appear on the link line first, before any libraries. Secondly you have to make sure that libraries are ordered correctly, so that the higher level libraries come earlier and the lower level libraries (that other libraries depend on) come afterwards.
You need to extract your library flags into two distinct parts: one part that has linker control options (such as -L) and another part that contains the actual libraries themselves (such as -l).
In standard makefiles these parts are placed into the variables LDFLAGS and LDLIBS, respectively:
LDFLAGS = -LD:/Development/OpenGL/Glew/glew-2.1.0/lib/Release/Win32 \
-LD:/Development/OpenGL/Glfw/lib-mingw
LDLIBS = -lglew32s -lglfw3 -lopengl32
I don't know enough about GLEW to know if the LDLIBS are ordered properly you may need to experiment.
Then, write your link rule like this:
$(BUILD)/test.exe: $(OBJ)
#echo LINKING $^
#$(CC) $(LDFLAGS) -o $# $^ $(LDLIBS)
For this reason I downloaded the C++ library VTK and made a local build in the build subdirectory on a OSX environment.
I would like to compile a project using this library (particularly I am using the class vtkSmartPointer) with Makefile.
Consider for example the following source code:
#include<iostream>
#include<vtkSmartPointer.h>
#include<vtkCallbackCommand.h>
int main()
{
vtkSmartPointer<vtkCallbackCommand> keypressCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
std::cout<<"hello world\n";
return 0;
}
For the Makefile I started from the second answer in this post to which I aded VTK library path:
CXX = g++
# OpenCV trunk
CXXFLAGS = -std=c++11 \
-I ../VTK/Common/Core/ -I ../VTK/build/Common/Core/ -I ../VTK/build/Utilities/KWIML/ \
-I ../VTK/Utilities/KWIML/ \
-L../VTK/build/lib \
-lvtkCommon -lvtkFiltering -lvtkImaging -lvtkGraphics -lvtkGenericFiltering -lvtkIO
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,%.o,$(SOURCES))
DEPENDS := $(patsubst %.cpp,%.d,$(SOURCES))
# ADD MORE WARNINGS!
WARNING := -Wall -Wextra
# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean
# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: parking
clean:
$(RM) $(OBJECTS) $(DEPENDS) parking
# Linking the executable from the object files
parking: $(OBJECTS)
$(CXX) $(WARNING) $(CXXFLAGS) $^ -o $#
-include $(DEPENDS)
%.o: %.cpp Makefile
$(CXX) $(WARNING) $(CXXFLAGS) -MMD -MP -c $< -o $#
My environment variable DYLD_LIBRARY_PATH has the value ../cmake_bin_dir/instDir/lib:../VTK/build/lib/.
When I try to compile running make, I get the following error:
ld: library not found for -lvtkCommon
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What part of the Makefile or program or step in the process is not correct?
Thank you in advance.
The current VTK library does not contain libVtkCommon.so (see package contents section https://www.archlinux.org/packages/community/x86_64/vtk/). Are you looking for libVtkCommonCore.so? If that is the case you have to change -lvtkCommon to -lvtkCommonCore in your Makefile. The same seems to be the case for some of the other included libraries.
here's my error
make
cc msgd.o -o msgd
msgd.o: In function `main':
/home/cam/Desktop/lab1/msgd.cc:37: undefined reference to `Server::Server(int, bool)'
/home/cam/Desktop/lab1/msgd.cc:39: undefined reference to `Server::~Server()'
msgd.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.7/iostream:75: undefined reference to `std::ios_base::Init::~Init()'
msgd.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
make: *** [msgd] Error 1
The first two undefined references to Server:: are bogus, the code works and Server.h is included properly in msgd.cc.
However when I simply do "make server" it runs fine, and when I do "make client" it runs fine, but I want to be able to just do "make" and have it build both.
Here's my makefile. According to https://stackoverflow.com/a/10907227/2080104 I need to include -lstdcc++ but I can't seem to figure out how to do so in a makefile.
# Makefile for socket examples
CXX= g++ $(CCFLAGS)
msgd= msgd.o Server.o Data.o User.o Message.o Str.o
msg= msg.o Client.o Str.o
OBJS = $(msgd) $(msg)
CCFLAGS= -std=c++11 -g
all: msgd Server Data User Message Str msg Client
server:$(msgd)
$(CXX) -o msgd $(msgd)
client:$(msg)
$(CXX) -o msg $(msg)
clean:
rm -f $(OBJS) $(OBJS:.o=.d)
realclean:
rm -f $(OBJS) $(OBJS:.o=.d) msgd msg
# These lines ensure that dependencies are handled automatically.
%.d: %.cc
$(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
| sed '\''s/\($*\)\.o[ :]*/\1.o $# : /g'\'' > $#; \
[ -s $# ] || rm -f $#'
include $(OBJS:.o=.d)
You named a variable msgd when you had an object file named msgd.o, and then you got them mixed up. Oh, and you have a target server which doesn't actually build server, it builds msgd.
You have a default target (all) which builds things other than what you want the default target to build.
You want to Make to do something (i.e. use -lstdcc++) when either you don't know how to do it on the command line, or you don't know which part of the makefile corresponds to what you do on the command line.
I'd suggest specific changes to the makefile, but your intent is so unclear I'm afraid I'd do more harm than good.
What happens is that target all requires msgd.
all: msgd Server Data User Message Str msg Client
You don't have a rule for msgd, however, make figures it can build msgd from msgd.o from msgd.cc (or whatever extension your source has) using the built-in rule:
Linking a single object file
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)’.
Your makefile should look more like the following:
CXX := g++
CXXFLAGS := -std=c++11 -g
CPPFLGS :=
LDFLAGS :=
LDLIBS := -g
all : msgd msg
msgd_obj := msgd.o Server.o Data.o User.o Message.o Str.o
msg_obj := msg.o Client.o Str.o
OBJ := ${msgd_obj} ${msg_obj}
msgd : ${msgd_obj}
msg : ${msg_obj}
# The rule to link the executables.
msgd msg :
${CXX} -o $# ${LDFLAGS} $^ ${LDLIBS}
# The rule to compile object files and produce dependencies.
%.o : %.cc
${CXX} -o $# ${CPPFLAGS} ${CXXFLAGS} -MD -MP $<
clean:
rm -f $(OBJS) $(OBJS:.o=.d)
realclean:
rm -f $(OBJS) $(OBJS:.o=.d) msgd msg
-include $(OBJS:.o=.d)
.PHONY: all clean realclean
Note that there should not be a specific rule to produce dependencies. Dependencies are produced as a by-product of compilation. On the first run dependencies are unnecessary because everything must be built anyway, on subsequent runs it uses dependencies from the previous run to determine what have changed since.
I'm working in windows and want to send data from C++ to Matlab. I've gotten the impression this is done most easy creating a makefile. Therefore I've installed cygwin to use the make command.
My makefile is as follows:
CXX = g++
CFLAGS = -O3 -I /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/include
LIBS = -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/
LIBS2 = -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/libmx.lib
LDFLAGS = -leng -lmx
RKspace2d: RKspace2d.o
$(CXX) -o $# $^ $(LDFLAGS) $(LIBS)
RKspace2d.o: RKspace2d.cpp
$(CXX) -c $(CFLAGS) $(LIBS) $<
# $# name of the target
# $^ name of all prerequisites with duplicates removed
# $< name of the first prerequisite
When I type in "make" in the cygwin terminal, being in the right directory I get the following error:
$ make
g++ -o RKspace2d RKspace2d.o -leng -lmx -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -leng
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lmx
collect2: ld returnerede afslutningskoden 1
makefile:8: recipe for target `RKspace2d' failed
make: *** [RKspace2d] Error 1
I believe the path is correct since both libeng.lib and libmx.lib are contained in the microsoft folder.
Hope you guys can help
Thomas