shared_ptr using gcc compiler - c++

I am trying to use shared_ptr in c++. I am using MinGw gcc compiler.
My g++ version is 4.8.1.
When I try to use std::shared_ptr, it says it does not name a type.
Is this because it is part of c++ 11, and that is not supported in the compiled I am using?
Or do I need to set a flag to use c++ 11 features?
EDIT: I tried adding the flag to enable c++ to my makefile, and it still gives an error.
My header where shared_ptr is used is as follows
#pragma once
#include <vector>
#include <memory>
namespace WavFileTools
{
class WavFile;
}
class WavFileTools::WavFile
{
public:
typedef std::vector<unsigned char> PCMData8_t;
typedef std::vector<unsigned short int> PCMData16_t;
struct WavFileHeader {
int num_channels;
int sample_rate;
int bits_per_sample;
};
static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename);
private:
WavFile(void);
private:
WavFileHeader m_header;
PCMData16_t m_data16;
PCMData8_t m_data8;
};
And the makefile...
CC := g++
CFLAGS := -g -O2 -std=c++0x
BIN_DIR := bin
BUILD_DIR := build
SRC_DIR := src
MAIN := WavFileTool
TARGET := wavfiletool.exe
SOURCES := $(wildcard src/*.cpp)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)
$(BIN_DIR)/$(TARGET): CREATE_DIRS $(BUILD_DIR)/$(MAIN).o $(OBJECTS)
$(CC) $(OBJECTS) $(CFLAGS) -o $#
$(BUILD_DIR)/$(MAIN).o: $(SRC_DIR)/WavFileTool.cpp
$(CC) $(CFLAGS) -c -o $# $<
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/%.h
$(CC) $(CC_FLAGS) -c -o $# $<
CREATE_DIRS:
if not exist $(BIN_DIR) mkdir $(BIN_DIR)
if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
CLEAN:
if exist $(BIN_DIR) rmdir /Q /S $(BIN_DIR) # /S allows deleting non-empty folder, and /Q prevents confirmation required
if exist $(BUILD_DIR) rmdir /Q /S $(BUILD_DIR)
and the error
In file included from src/WavFile.cpp:1:0:
src/WavFile.h:26:10: error: 'shared_ptr' in namespace 'std' does not name a type
static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename);

Related

Initialisation in Singleton

So, I am creating a small testing library for some simple tasks. I use the self-registration method to define the tests, but I am getting a segfault that I don't understand where is coming from. My project looks like this
Lib
|
|__include
| |__lib.hpp
|__src
| |__lib.cpp
|__examples
| |__example.cpp
|__Makefile
This is my .hpp. Please, ignore the public maps, as I am well aware that they should not be public in the final version.
#include <string>
#include <map>
#include <functional>
#define TEST(name, result_var_name, short_result_var_name, explanation_var) \
bool test_##name(std::string result_var_name); \
static bool test_##name##_registered = TestFactory::getInstance()->Register(#name, &test_##name, #result_var_name, #short_result_var_name); \
static bool test_##name##_description = TestFactory::getInstance()->RegisterExplanation(#result_var_name, explanation_var); \
bool test_##name(std::string result_var_name)
#define RESULT_LONG(result_var_name, result) \
TestFactory::getInstance()->RegisterResult(result_var_name, result);
#define RESULT_SHORT(result_var_name, result) \
TestFactory::getInstance()->RegisterResult(TestFactory::getInstance()->short_to_long_param[result_var_name], result);
class TestFactory {
private:
TestFactory();
public:
std::map<std::string, std::function<bool(std::string)>> Tests;
std::map<std::string, std::string> short_to_long_param;
std::map<std::string, std::string> param_to_testName;
std::map<std::string, std::string> testName_to_result;
std::map<std::string, std::string> param_to_explanation;
static TestFactory* getInstance();
bool Register(std::string name, std::function<bool(std::string)> func, std::string long_param, std::string short_param);
bool RegisterResult(std::string result_var_name, std::string result);
bool RegisterExplanation(std::string result_var_name, std::string explanation);
};
And this is my example file:
#include "lib.hpp"
#include <iostream>
TEST(testing_test, foo, f, "This is a description of foo"){
try
{
return foo == "example";
}
catch(const std::exception& e)
{
std::cerr << "Exception thrown in testing_test: " << e.what() << '\n';
return false;
}
};
It was working when I built everything together manually, but then I decided to create the static library and compile the example separately, it started segfaulting.
This is what my Makefile looks like (as you can see, I am not a pro Makefile user, please be gentle)
LIBNAME := libtest.a
CXX := g++
BIN := bin
SRC := src
INCLUDE := include
LIB := lib
LIBRARIES := -ltest
EXECUTABLE := example
LIBRARY_SOURCES := $(SRC)/lib.o
EXECUTABLE_SOURCES := examples/example.o
CXXFLAGS := -Wall -Wextra -std=c++17 -ggdb -I$(INCLUDE)
all: $(BIN)/$(EXECUTABLE)
run: clean all
clear
./$(BIN)/$(EXECUTABLE)
$(LIB)/$(LIBNAME): $(LIBRARY_SOURCES)
ar rcs $# $^
$(BIN)/$(EXECUTABLE): $(LIB)/$(LIBNAME)
$(CXX) $(CXXFLAGS) -I$(INCLUDE) -L$(LIB) $(EXECUTABLE_SOURCES) -o $# $(LIBRARIES)
clean:
-rm -rf $(BIN)/*
-rm -rf $(LIB)/*
Initially, I had all static methods and variables, and I changed them to a singleton to avoid initialisation and memory issues. However, I am getting a segfault in the Register method when it is trying to access the Test map. Any ideas?
EDIT:
So, I reproduced the compilation manually like so:
g++ -Iinclude -std=c++17 -c src/mxIntegration.cpp -o lib/manual_object_file.o
ar rvs lib/manual_library.a lib/manual_object_file.o
g++ -Iinclude examples/main.cpp lib/manual_object_file.o -o bin/manual_test_linking
The resulting executable is running. I'm going to try to catch any difference in the process now.
So, after careful analysis, it seems like the example.o file was not being generated successfully. I basically added them as a requirement for the executable recipe, and updated the clean recipe. I believe the .a file could also be passed as an input to the final g++ command.
LIBNAME := libIntegration.a
CXX := g++
BIN := bin
SRC := src
INCLUDE := include
LIB := lib
LIBRARIES := -l:$(LIBNAME)
EXECUTABLE := main
LIBRARY_SOURCES := $(SRC)/mxIntegration.o
EXECUTABLE_SOURCES := examples/main.o
CXXFLAGS := -Wall -Wextra -std=c++17 -ggdb -I$(INCLUDE)
all: $(BIN)/$(EXECUTABLE)
run: clean all
clear
./$(BIN)/$(EXECUTABLE)
$(LIB)/$(LIBNAME): $(LIBRARY_SOURCES)
ar rvs $# $^
$(BIN)/$(EXECUTABLE): $(LIB)/$(LIBNAME) $(EXECUTABLE_SOURCES)
$(CXX) $(CXXFLAGS) -L$(LIB) $(EXECUTABLE_SOURCES) -o $# $(LIBRARIES)
clean:
-rm -rf $(BIN)/*
-rm -rf $(LIB)/*
-rm -rf **/*.o

Calling function from other C++ source doesn't work

I've looked at several posts of the same questions on here and as far as I've figured I've done what they said to do. However, I still get a "undefined reference to `cmb::functionA()'" warning.
I have the header:
//combine.h
#ifndef COMBINE_H
#define COMBINE_H
namespace cmb
{
void functionA();
}
#endif
Function source file:
// combine.cc
#include <iostream>
#include "combine.h"
using namespace std;
namespace cmb
{
void functionA()
{
cout << "print something\n";
}
}
And main:
//main.cc
#include "combine.h"
using namespace std;
using namespace cmd;
int main(int argc, char *argv[])
{
functionA();
}
It is now working when compiling manually (g++ -o Test *.cc -Wall --std=c++17) but using make still gives me the same error. I really don't understand make files so any help would be appreciated.
makefile:
CXX := g++
CXXFLAGS += -Wall -std=c++17
LIBSRCS = $(filter-out main.cc,$(shell find -name \*.cc))
LIBOBJS = $(patsubst %.cc,%.o,$(LIBSRCS))
main: main.o combine.o libproject.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $# $<
$(LIBOBJS): %.o: %.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $# $<
libproject.a: $(LIBOBJS)
ar rcs $# $^
clean:
rm -f libproject.a $(LIBOBJS)
.PHONY: clean
I just use make main in terminal.
You must add combine.o after main: in the makefile.
Since you use a library, you need to tell the linker to use it (LDFLAGS), and it should be after the main in g++ command. As in previous comments, the using namespace cmd needed to be changed to cmb
This one worked for me:
CXX := g++
CXXFLAGS += -Wall -std=c++17
LIBSRCS = $(filter-out ./main.cc,$(shell find -name \*.cc))
LIBOBJS = $(patsubst %.cc,%.o,$(LIBSRCS))
LDFLAGS += -L. -lproject
main: main.o libproject.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $# $< $(LDFLAGS)
libproject.a: $(LIBOBJS)
ar rcs $# $^
.PHONY: clean
clean:
rm -f libproject.a $(LIBOBJS) main main.o
I also needed to add ./ in filtering out main.cc
Example run:
jontte#jontte-Latitude-E5420:~/Temp/maketest$ make
g++ -Wall -std=c++17 -c -o main.o main.cc
g++ -Wall -std=c++17 -c -o combine.o combine.cc
ar rcs libproject.a combine.o
g++ -Wall -std=c++17 -o main main.o -L. -lproject
jontte#jontte-Latitude-E5420:~/Temp/maketest$ ./main
print something
jontte#jontte-Latitude-E5420:~/Temp/maketest$ make clean
rm -f libproject.a ./combine.o main main.o
jontte#jontte-Latitude-E5420:~/Temp/maketest$

Why is codeblocks not respecting the breakpoints?

I execute with debugger this code in codeblocks but the breakpoints are not respected. Why is this? The problem persists if I change the location of the breakpoint. When running the script with the default Codeblocks makefile everything debiggs ok. When changing to the makefile below the debugger sees the breakpoints only when dleting the "typedef unsigned char byte;".
#include <iostream>
using namespace std;
typedef unsigned char byte;
int main(const int argc, const char *const argv[])
{
return 0;
}
I use this makefile:
CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS = -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC) $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
$(CXX) -o $# $^ $(LDFLAGS)
%.o : %.cpp $(HDR)
$(CXX) $(CXXFLAGS) $< -o $#
clean :
rm -f $(OBJ) test
The code runs without errors, only that I can't use the debugger.
Should I add in the makefile sth. about typedef command?

Linked error/Makefile

I am sure this is stupid but i get this linker error
undefined reference to insert_record ,which is a function declared in Operations.hpp,implemented in Operations.cpp and used in main, and i can't find what is wrong in this makefile.
Maybe a pair of rested eyes can spot the problem.
It seems that operations isn't linked although the object is being created.
OBJS = main.o Operations.o CDR.o TBucketList.o RBucketList.o DateTime.o HashTable.o
CC = g++
CXXFLAGS = -W -Wall -Wno-unused -pedantic -c -g
LDFLAGS = -W -Wall -Wno-unused -pedantic -g
werhaus: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o werhaus
main.o : CDR.hpp Operations.hpp HashTable.hpp
$(CC) $(CXXFLAGS) main.cpp
Operations.o : Operations.hpp HashTable.hpp
$(CC) $(CXXFLAGS) Operations.cpp
TBucketList.o : TBucketList.hpp RBucketList.hpp
$(CC) $(CXXFLAGS) TBucketList.cpp
RBucketList.o : RBucketList.hpp
$(CC) $(CXXFLAGS) RBucketList.cpp
HashTable.o : HashTable.hpp TBucketList.hpp
$(CC) $(CXXFLAGS) HashTable.cpp
CDR.o : CDR.hpp DateTime.hpp
$(CC) $(CXXFLAGS) CDR.cpp
DateTime.o : DateTime.hpp
$(CC) $(CXXFLAGS) DateTime.cpp
clean:
\rm *.o werhaus
Update:
main.cpp :
insert_record(originator_number, aCDR, destination_number, hashtable1, hashtable2, BucketSize);
Operations.hpp:
void insert_record(const char*, CDR*, const char*, const Hashtable*, const Hashtable*, int);
Operations.cpp:
void insert_record(const char* originator_number, CDR* aCDR, const char* destination_number, Hashtable* hashtable1, Hashtable* hashtable2, int Bsize)
All seem fine in main..
There does not seem to be an error in your Makefile. As #G.M. noted, the dependencies of .o to corresponding .cpp are missing, these need to be added.
It can be that insert_record declaration in Operations.hpp does not match the definition in Operations.cpp and you end up with two functions insert_record. Check that the function declaration and definitions match: same return type and argument types, both in the same namespace.
I think you should better use something like this:
NAME= werhaus
CXX= g++ # Use CXX for C++
CXXFLAGS= -W -Wall -Wno-unused -pedantic -g
LDFLAGS= -g # No need for warning flags at linking
SRC= main.cpp \
Operations.cpp \
CDR.cpp \
TBucketList.cpp \
RBucketList.cpp \
DateTime.cpp \
HashTable.cpp
OBJS= $(SRC:.cpp=.o)
$(NAME): $(OBJS)
$(CXX) $(LDFLAGS) $(OBJS) -o $(NAME)
all: $(NAME)
%.o: %.cpp
$(CXX) -o $# -c $< $(CXXFLAGS)
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re
This type of makefile will avoid you a lot of errors.
Also, you don't need to put hpp files as dependencies.
Good luck.
In Operations.hpp you have a declaration with signature:
void insert_record(
const char*, CDR*,
const char*,
const Hashtable*,
const Hashtable*,
int)
In Operations.cpp you define a function with the different signature:
void insert_record(
const char* CDR*,
const char*,
Hashtable*,
Hashtable*,
int)
So the declaration in Operations.hpp is not in fact implemented in Operations.cpp
and is undefined.

C++ Variable length array (VLA) warnings

I read different answers about VLA on SO but couldn't find the answer. In my case, I have one function that allocates memory:
template<typename T>
void allocMemory(T *&data, const size_t numElems)
{
#ifdef PINNED_MEMORY
// allocate pinned memory
#else
data = new T[numElems];
#endif
}
Now, I have a vector class where I use this method:
template<typename T>
class MyVec
{
T *data;
size_t size;
public:
MyVec(size_t _size): size(_size)
{ allocMemory<T>(data, size); } // gives VLA warning
};
It happens when I compile it using nvcc (V0.2.1221) compiler which I guess uses gcc compiler underneath (?) The actual warning is:
myvec.h:16:6: warning: ISO C++ does not support variable-length array types [-Wvla]
data = new T[numElems];
I think you don't compile your project the right way.
Try to using the flowing make file.
CUDA_INSTALL_PATH := /usr/local/cuda
CXX := g++
CC := gcc
LINK := g++ -fPIC
NVCC := nvcc
#Includes
INCLUDES = -I. -I$(CUDA_INSTALL_PATH)/include
#Common flags
COMMONFLAGS += $(INCLUDES)
NVCCFLAGS += $(COMMONFLAGS)
CXXFLAGS += $(COMMONFLAGS)
CFLAGS += $(COMMONFLAGS)
LIB_CUDA := -L$(CUDA_INSTALL_PATH)/lib -lcudart
#OBJS = GpuSolver.cu.o main.cpp.o
OBJS = main.cu.o a.cpp.o # your files
TARGET = a.out
LINKLINE = $(LINK) -o $(TARGET) $(OBJS) $(LIB_CUDA)
.SUFFIXES: .c .cpp .cu .o
%.c.o: %.c
$(CC) $(CFLAGS) -c $< -o $#
.cu.o: %.cu
$(NVCC) $(NVCCFLAGS) -c $< -o $#
%.cpp.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $#
$(TARGET): $(OBJS) "makefile" #your makefile file name
$(LINKLINE)