boost makefile on Mac OS X - c++

I'd like to use boost regex library and created a very short program to test my makefile
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main() {
regex exp("test");
cout << "Hello World" << endl;
}
Here is my makefile (I've got the boost includes from this thread Including boost libraries in make files)
EXEC = main
SOURCES = $(wildcard *.cpp)
HEADERS = $(wildcard *.h*)
OBJECTS = $(SOURCES:.cpp=.o)
all: $(EXEC)
main: $(OBJECTS)
g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_filesystem-mt -lboost_thread-mt $(OBJECTS) -o $(EXEC)
%.o: %.cpp $(HEADERS)
g++ -I/usr/local/Cellar/boost/1.54.0/include -c $< -o $#
clean:
rm -f $(EXEC) $(OBJECTS)
When I compile my program a get the following error message:
g++ -I/usr/local/Cellar/boost/1.54.0/include -c main.cpp -o main.o
g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_filesystem-mt -lboost_thread-mt main.o -o main
Undefined symbols for architecture x86_64:
"boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
What is missing?
I've installed boost with homebrew under Mac OS X 10.8.

You are missing a link to the boost_regex_mt library
-lboost_filesystem-mt -lboost_thread-mt
In your makefile...
main: $(OBJECTS)
g++ -L/usr/local/Cellar/boost/1.54.0/lib -lboost_regex-mt -lboost_filesystem-mt -lboost_thread-mt $(OBJECTS) -o $(EXEC)

Related

Issue with the linker using opencv and C++ - Undefined Reference

There are a couple similar problems to this but they all seem to have answers that don't work.
I've installed opencv4 onto an image of Ubuntu and I know the opencv files are install correctly
$ pkg-config --cflags opencv
-I/usr/include/opencv
and have a proper .cp file
/usr/local/lib/pkgconfig$ ls
gpr.pc grpc++.pc grpc++_unsecure.pc protobuf-lite.pc
grpc.pc grpc_unsecure.pc opencv4.pc protobuf.pc
But even though it's all installed correctly it's giving me this error.
~/neuralink/neuralink-image-service-prompt/proto$ make g++ -std=c++11 `pkg-config --cflags opencv protobuf grpc` -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl foo.cpp -o foo
/tmp/ccnfoOEl.o: In function `main':
foo.cpp:(.text+0x48): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccnfoOEl.o: In function `cv::String::String(char const*)':
foo.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccnfoOEl.o: In function `cv::String::~String()':
foo.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::String::operator=(cv::String const&)':
foo.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccnfoOEl.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'foo' failed
make: *** [foo] Error 1
Some people have already tried answering similar questions (such as Undefined reference to cv::imread(cv::String const&, int) and linking opencv libraries with g++) however these don't help, as the user didn't add opencv to their makefile and I have.
Here is my make makefile
LDFLAGS = -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
CXX = g++
CPPFLAGS += `pkg-config --cflags opencv protobuf grpc`
CXXFLAGS += -std=c++11
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
all: foo
client: image.pb.o image.grpc.pb.o client.o
$(CXX) $^ $(LDFLAGS) -o $#
server: image.pb.o image.grpc.pb.o server.o
$(CXX) $^ $(LDFLAGS) -o $#
%.grpc.pb.cc: %.proto
protoc --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
protoc --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h client server
And I wrote a simple program that still throws the error
#include <opencv2/opencv.hpp>
#include <stdio.h>
int main() {
cv::Mat frame = cv::imread("sylvania.png", cv::IMREAD_UNCHANGED);
printf("Image size is %i x %i\n", frame.rows, frame.cols);
}
Everything seems to compile fine but I keep getting this linker issue. I don't seem to get the issue when I compile it on another computer (my mac) for what it's worth. Also I have the gRPC and Protoc files because I need them for the project, I just didn't use those file as an example since they're quite long.
I agree with Micka, here is a Makefile of one of my projects:
CPPFLAGS=-g -Wall -I. -DDEBUG
LDFLAGS=-g
LDLIBS=-lopencv_core -lopencv_calib3d -lopencv_highgui -lopencv_imgproc -lopencv_stitching -lopencv_video
main: main.o AffineKalmanLP.o
g++ $(LDFLAGS) -o main main.o AffineKalmanLP.o $(LDLIBS)
main.o: main.cpp main.h
g++ $(CPPFLAGS) -c main.cpp
AffineKalmanLP.o: AffineKalmanLP.cpp AffineKalmanLP.h
g++ $(CPPFLAGS) -c AffineKalmanLP.cpp
clean:
rm main main.o AffineKalmanLP.o
You need to add it to LDLIBS.
If I remove that line, I get undefined reference for various things as well.

aws-sdk-cpp: unresolved symbols

I am trying to build a simple example using aws sdk cpp. But I am stumbled on a building step. I am linking libaws-cpp-sdk-s3.so library, which is supposed to have all symbols from the source file. But the linker cannot even find a couple of them. The source file is:
#include <aws/core/Aws.h>
int main( int argc, char ** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// make your SDK calls here.
}
Aws::ShutdownAPI(options);
return 0;
}
by using this Makefile:
CC = g++
CFLAGS = -g -c -Wall -std=c++11
LDFLAGS = -g
EXECUTABLE = ex1
RM = rm -f
SOURCES = main.cpp
OBJS = $(SOURCES:.cpp=.o)
all: $(EXECUTABLE)
$(EXECUTABLE): main.o -laws-cpp-sdk-s3
$(CC) $(LDFLAGS) main.o -o $#
main.o: main.cpp
$(CC) $(CFLAGS) $^ -o $#
.PHONY: clean
clean:
$(RM) $(EXECUTABLE) $(OBJS) $(SOURCES:.cpp=.d)
When I run make, I got this error. But why? I built
g++ -g main.o -o ex1
main.o: In function main':
/home/username/workspace/ex1/src/main.cpp:6: undefined reference toAws::InitAPI(Aws::SDKOptions const&)'
/home/username/workspace/ex1/src/main.cpp:12: undefined reference to `Aws::ShutdownAPI(Aws::SDKOptions const&)'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'ex1' failed
make: *** [ex1] Error 1
I don't see where you are linking libaws-cpp-sdk-core
You probably need:
$(EXECUTABLE): main.o -laws-cpp-sdk-s3 -laws-cpp-sdk-core
$(CC) $(LDFLAGS) main.o -o $#

Linking dynamic library to my project

After reading lots of post I am really confused.
I want to link a dynamic library to my cpp code.
The library is in /usr/local/include/sbml
and the libsbml.so file can be found in /usr/local/lib
I have a makefile that looks like this
SHELL = /bin/sh
VERSION = 5.04.02
CC = /usr/bin/g++
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
LDFLAGS = -lsbml
OBJ = main.o SBML.o
prog: $(OBJ)
$(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY : clean
clean :
-rm edit $(OBJ)
If I run the makefile I get the following error: (undefined reference to SBMLReader::readSBMLFromFile())
g++ -c -o SBML.o SBML.cpp
/usr/bin/g++ -Wall -g -D_REENTRANT -DVERSION=\"5.04.02\" -o prog main.o SBML.o -lsbml
sbml.o: In Funktion `SBML::readSBML()':
sbml.cpp:(.text+0x129): Nicht definierter Verweis auf `SBMLReader::readSBMLFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
The library was not linked because you need to specify the folder containing the so-file as well:
LDFLAGS = -L/usr/local/lib -lsbml
Now it should link and your unresolved symbol should be gone as well.

C++/OpenGL - Starting a new project

I'm starting a new project using C++ and OpenGL to make a simple game and to teach myself OpenGL. I do not want to use GLUT, and would rather use SDL for making windows and such. I'm having a lot of weird trouble with the Makefile, however. Currently, when I type make, I get this response when I include $(LFLAGS) $(LDFLAGS) in my G++ commands:
g++-5 tetris.o -o tetris -std=c++14
Undefined symbols for architecture x86_64:
"_SDL_CreateWindow", referenced from:
_main in tetris.o
"_SDL_GL_CreateContext", referenced from:
_main in tetris.o
"_SDL_Init", referenced from:
_main in tetris.o
"_SDL_PollEvent", referenced from:
game_loop(SDL_Window*) in tetris.o
"_glewGetErrorString", referenced from:
_main in tetris.o
"_glewInit", referenced from:
_main in tetris.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [tetris] Error 1
and this response when I do not:
g++-5 -Wall -framework SDL2 tetris.o -o tetris -std=c++14
ld: framework not found SDL2
collect2: error: ld returned 1 exit status
make: *** [tetris] Error 1
Here is my new Makefile:
CC = g++-5
CFLAGS = -Wall -c -Wno-deprecated-declarations
LFLAGS = -Wall
LDFLAGS = -framework SDL2
LDLIBS= -lSDL2 -lglew -lGL
INCLUDES = -I/Library/Frameworks/SDL2.framework/Headers/
STD = -std=c++14
all: tetris
tetris: tetris.o
$(CC) $(LFLAGS) $(LDFLAGS) tetris.o -o tetris $(STD)
tetris.o: tetris.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -o $# tetris.cpp $(STD)
.PHONY: clean
clean:
rm tetris *.o
And the headers part of my .cpp file:
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <unistd.h>
#include <iostream>
maybe you need to add
LDFLAGS = -framework GLUT
to your Makefile
Just use freeglut, you don't need to use SDL so that you can get started with OpenGL.

Linking Errors tinyxml on OS X 10.9.2

I would like to use tinyxml. I have used it in the past, and it works great. The only problem is I was developing on my Linux box which is a laptop. I have a mac mini and I have that set up as my desktop, so I would like to use it for a big chunk of development.
I have a simple tinyxml example that one of my instructors gave me. With this example came a Makefile like so:
ifeq ("$(shell whoami)", "malloy")
CXX = clang++
else
CXX = g++
endif
# Warnings frequently signal eventual errors:
CXXFLAGS=`sdl-config --cflags` -g -W -Wall -Weffc++ -Wextra -pedantic -O0
ifeq ("$(shell uname)", "Darwin")
LDFLAGS = -framework Foundation -framework GLUT -framework OpenGL -lm
else
ifeq ("$(shell uname)", "Linux")
LDFLAGS = `sdl-config --libs` -lm -lSDL_ttf -lSDL_image -ltinyxml
endif
endif
OBJS = \
main.o
EXEC = run
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $#
$(EXEC): $(OBJS)
$(CXX) $(CXXFLAGS) -o $# $(OBJS) $(LDFLAGS)
main.o: main.cpp
clean:
rm -rf $(OBJS)
rm -rf $(EXEC)
And when I try to make this simple project I get all of these Undefined symbol errors.
This is what terminal outputs after I type make:
g++ `sdl-config --cflags` -g -W -Wall -Weffc++ -Wextra -pedantic -O0 -o run main.o - framework Foundation -framework GLUT -framework OpenGL -lm
Undefined symbols for architecture x86_64:
"TiXmlString::nullrep_", referenced from:
TiXmlString::quit() in main.o
"TiXmlDocument::LoadFile(TiXmlEncoding)", referenced from:
_main in main.o
"TiXmlDocument::TiXmlDocument(char const*)", referenced from:
_main in main.o
"TiXmlNode::Clear()", referenced from:
_main in main.o
"TiXmlNode::~TiXmlNode()", referenced from:
TiXmlDocument::~TiXmlDocument() in main.o
"TiXmlElement::Attribute(char const*) const", referenced from:
_main in main.o
"TiXmlNode::FirstChildElement() const", referenced from:
TiXmlNode::FirstChildElement() in main.o
"TiXmlNode::NextSiblingElement() const", referenced from:
TiXmlNode::NextSiblingElement() in main.o
"vtable for TiXmlDocument", referenced from:
TiXmlDocument::~TiXmlDocument() in main.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 1
I know it is a linker error that is about it. I used Homebrew to install tinyxml. It was actually giving me problems so after I got the tar-ball for tinyxml I extracted it and put the folder in /usr/local/include. g++ doesn't complain about finding the files. Just stuff with the v-table.
Any help is greatly appreciated! Thanks in advance!!!
You should modify the makefile and instruct the linker to use tinyxml (-ltinyxml) in the "Darwin" case. AFAIK, tinyxml is not bundled with OSX so you may need to find or build that. Instead, you can use expat (simpler-easier) or libxml2 (faster-detailed), which already come with OSX (you can also use (c++) wrappers, google them...)