Im working on making a simulator with c++, for which I need to read files.
my directory looks something like this
proj
------>bin #stores the executable
------>include #stroes the external library includefiles
------>lib #stores the lib files of the libraries
------>obj #stores the .o files
------>src #source files
makefile
my make file looks this
CC = g++
OUT = chip8
ODIR = ./obj
SDIR = ./src
OUTDIR = ./bin
IDIR = ./include
LDIR = ./lib
libs = -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
OBJS = $(patsubst $(SDIR)/%.cpp,$(ODIR)/%.o,$(wildcard $(SDIR)/*.cpp))
vpath %.cpp $(SDIR):$(SDIR)/Chip8
$(ODIR)/%.o : %.cpp
$(CC) -c -I $(IDIR) -o $# $^
$(OUTDIR)/% : $(wildcard obj/*.o)
$(CC) -L $(LDIR) -o $# $^ $(libs)
.PHONY : run
run :
$(OUTDIR)/$(OUT)
these are my source files
main.cpp
#include <iostream>
#include "Chip8/RomFileReader.h"
int main(){
RomReader romReader;
if(romReader.OpenFile("picture.ch8") == -1){
std::cout<<"could not open file !"<<std::endl;
return EXIT_FAILURE;
}
romReader.GetRom();
uint8_t * rom = romReader.ReturnRom();
int size = romReader.GetRomSize();
for(int i = 0; i<size; i++)
std::cout<<rom[i]<<std::endl;
free(rom);
romReader.FreeRom();
romReader.CloseReader();
}
ReadRomFile.h
#pragma once
#include <iostream>
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
class RomReader{
private :
FILE * m_Reader;
uint8_t * m_Rom;
public :
int OpenFile(const char * fileName);
void GetRom();
void FreeRom();
uint8_t * ReturnRom();
void CloseReader();
int GetRomSize();
};
RomFileReader.cpp
#include "RomFileReader.h"
int RomReader :: OpenFile(const char * fileName){
m_Reader = fopen(fileName,"rb");
if(m_Reader == NULL){
return -1;
} else
return 1;
}
int RomReader :: GetRomSize(){
int start = ftell(m_Reader);
fseek(m_Reader,0,SEEK_END);
int end = ftell(m_Reader);
fseek(m_Reader,0,SEEK_SET);
int size = end - size;
return size;
}
void RomReader :: GetRom(){
int size = GetRomSize();
if(m_Rom == NULL){
m_Rom = new uint8_t[size];
}
fread(m_Rom,1,size,m_Reader);
}
void RomReader :: FreeRom(){
free(m_Rom);
}
uint8_t * RomReader :: ReturnRom(){
return m_Rom;
}
void RomReader :: CloseReader(){
fclose(m_Reader);
}
this is the error I'm getting
./bin/chip8
could not open file !
make: *** [run] Error 1
I could use fstream but I'm more comfortable and confident in using FILE instead, I had done something similar in c and it worked without any issue.
I'm really not able to point at what is exactly not working.
my picture.ch8 is in the bin folder along with the executable, yet I get this error. What is it that I'm missing exactly?
Your main is calling
romReader.FreeRom();
I think m_Rom is not NULL. So the memory get freed, so the memory exception getting fired?!?
Set it to NULL in a constructor of your class:
class RomReader {
...
public :
RomReader() { m_Rom = NULL; };
~RomReader() { if ( m_Rom != NULL ) delete [] m_Rom; };
...
}
The basic problem you have is that wildcard expands to the files that exist when make reads the makefile. So when you build with a clean tree (where the obj directory is empty), it expands to nothing, so nothing gets built.
The upshot is that wildcard cannot be usefully used with intermediate files generated as part of your build as they might not exist yet when you build. It is only useful for finding source files.
You need instead something like
$(OUTDIR)/$(OUT): $(patsubst src/%.cpp, obj/%.o, $(wildcard src/*.cpp)) $(patsubst src/Chip8/%.cpp, obj/%.o, $(wildcard src/Chip8/*.cpp))
You also probably want to have your run target depend on the executable
run: $(OUTDIR)/$(OUT)
otherwise it will not (re)build it when you try to run.
Related
I need to solve a nonlinear system of equations and I found the MINPACK library to do the job.
But I need the code to work with CUDA and I'm not able to compile the code.
I'm facing this problem:
nvcc -arch sm_61 -Iinc -I/usr/local/include obj/main.o -o oi -L/usr/local/lib64 -lcuminpack
nvlink error : Undefined reference to 'dpmpar' in 'obj/main.o'
make: *** [makefile:55: oi] Erro 255
My main file is:
main.cu
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define __cminpack_double__
#include <cminpack-1/cminpack.h>
#define real __cminpack_real__
typedef struct{
real fnorm1, fnorm2;
int info;
real solution[2];
} ResultType;
__cminpack_attr__ int fcn(void *p, int n, const real *x, real *fvec, int iflag){
--fvec;
--x;
fvec[0] = x[0] + x[0]*x[1] - 4.0;
fvec[1] = x[0] + x[1] -3.0;
return 0;
}
__global__ void SolvingSystem(ResultType *pResults){
int info;
real fnorm1, fnorm2, tol;
int n = 2;
real fvec[2];
real x[2];
const int lwa = (n*(3*n + 13))/2;
real wa[50];
tol = sqrt(dpmpar(1));
x[0] = 1.98;
x[1] = 1.02;
printf("Initial Guess: %g, %g\n", x[0], x[1]);
}
int main(int argc, char const *argv[]){
cudaSetDevice(0);
ResultType *reuslt_GPU, *reuslt_CPU;
cudaMalloc((void**)&reuslt_GPU, sizeof(ResultType));
cudaMallocHost((void**)&reuslt_CPU, sizeof(ResultType));
printf("Solving System...\n");
dim3 grid(1, 1, 1);
dim3 block(1, 1, 1);
SolvingSystem<<< grid, block>>>(reuslt_GPU);
cudaDeviceSynchronize();
printf("Done!\n");
return 0;
}
And my makefile is:
makefile
# MakeFile
# Program Name
EXE = oi
# Compilers
NVCC := nvcc
ALL_LIBRARIES := cuminpack
LIBDIR := /usr/local/lib64
# Directories
SRCDIR := src
OBJDIR := obj
INCDIR := inc /usr/local/include
#Flags
NVCCARCHFLAG := -arch sm_61
NVCCFLAGS := -std=c++11
LDFLAGS := $(addprefix -L, $(LIBDIR))
INCLUDES := $(addprefix -I, $(INCDIR))
LIBRARIES := $(addprefix -l, $(ALL_LIBRARIES))
LIBRARIES +=
ALL_CPFLAGS := -MMD
ALL_CPFLAGS += $(NVCCARCHFLAG)
ALL_CPFLAGS += $(NVCCFLAGS)
ALL_LDFLAGS := $(LDFLAGS)
# Files
C_FILES := $(wildcard $(SRCDIR)/*.c)
CU_FILES := $(wildcard $(SRCDIR)/*.cu)
C_OBJ := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(C_FILES))
CU_OBJ := $(patsubst $(SRCDIR)/%.cu, $(OBJDIR)/%.o, $(CU_FILES))
C_DEP := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.d, $(C_FILES))
CU_DEP := $(patsubst $(SRCDIR)/%.cu, $(OBJDIR)/%.d, $(CU_FILES))
SRC := $(C_FILES) $(CU_FILES)
OBJ := $(C_OBJ) $(CU_OBJ)
DEP := $(C_DEP) $(CU_DEP)
COMPILE.c := $(NVCC) -MMD -g $(INCLUDES) -c
COMPILE.cu := $(NVCC) $(ALL_CPFLAGS) $(INCLUDES) -dc
.PHONY: all clean
all:$(EXE)
$(EXE): $(OBJ)
$(NVCC) $(NVCCARCHFLAG) $(INCLUDES) $^ -o $# $(ALL_LDFLAGS) $(LIBRARIES)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(COMPILE.c) $< -o $#
$(OBJDIR)/%.o: $(SRCDIR)/%.cu | $(OBJDIR)
$(COMPILE.cu) $< -o $#
$(OBJDIR):
#mkdir -p $#
# Cleaning up
clean:
#echo Cleaning up...
#rm -f -r $(OBJDIR)
#rm $(EXE)
# Including dependency
-include $(DEP)
The main file needs to be finished and that's the reason to some unused variables.
I just can't figure out what is causing this error, I think the problem is in cuminpack library, but I don't now how to fix it.
I'm using cminpack-1.3.8 from this website: http://devernay.free.fr/hacks/cminpack/
I installed the library using cmake then make and make install. Then I went to source folder and did make cuda. This last command generated libcuminpack.a file in cuda directory, then I copied this .a file to /usr/local/lib64 where cmake installed the other libraries files.
If anyone could help me I would be very thankful
EDIT
Files from the cminpack library
cminpack.h
...
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
...
#if defined(__CUDA_ARCH__) || defined(__CUDACC__)
#define __cminpack_attr__ __device__
#ifndef __cminpack_real__
#define __cminpack_float__
#define __cminpack_real__ float
#endif
#define __cminpack_type_fcn_nn__ __cminpack_attr__ int fcn_nn
#define __cminpack_type_fcnder_nn__ __cminpack_attr__ int fcnder_nn
#define __cminpack_type_fcn_mn__ __cminpack_attr__ int fcn_mn
#define __cminpack_type_fcnder_mn__ __cminpack_attr__ int fcnder_mn
#define __cminpack_type_fcnderstr_mn__ __cminpack_attr__ int fcnderstr_mn
#define __cminpack_decl_fcn_nn__
#define __cminpack_decl_fcnder_nn__
#define __cminpack_decl_fcn_mn__
#define __cminpack_decl_fcnder_mn__
#define __cminpack_decl_fcnderstr_mn__
#define __cminpack_param_fcn_nn__
#define __cminpack_param_fcnder_nn__
#define __cminpack_param_fcn_mn__
#define __cminpack_param_fcnder_mn__
#define __cminpack_param_fcnderstr_mn__
...
__cminpack_attr__
__cminpack_real__ CMINPACK_EXPORT __cminpack_func__(dpmpar)( int i );
dpmpar.c
#include "cminpack.h"
#include <float.h>
#include "cminpackP.h"
#define DPMPAR(type,X) _DPMPAR(type,X)
#define _DPMPAR(type,X) type ## _ ## X
__cminpack_attr__
real __cminpack_func__(dpmpar)(int i)
{
/* ********** */
/* Function dpmpar */
...
Based on talonmies's comment I was able to compile the code defining the __device__ function dpmpar in main.cu file.
Then I found this post https://forums.developer.nvidia.com/t/external-calls-to-device-functions/17737 where the guy answered that CUDA doesn't have a linker on the device side, so it's not possible to call a __device__ function from a different .cu file.
With this in mind, I copied only the files that I needed from the library and made some modifications, like changing __cminpack_attr__ to __device__ and I rewrote the header file too. So basically I made my own device library (thank's again to talonmies's).
The last thing I did was move all this new files to the same folder where my main.cu and my makefile lives and I rewrote the makefile.
Now the code compile fine!!
I'm experimenting with building a simple application from a couple of .cu source files and a very simple C++ main that calls a function from one of the .cu files. I'm making a shared library (.so file) from the compiled .cu files. I'm finding that everything builds without trouble, but when I try to run the application, I get a linker undefined symbol error, with the mangled name of the .cu function I'm calling from main(). If I build a static library instead, my application runs just fine. Here's the makefile I've set up:
.PHONY: clean
NVCCFLAGS = -std=c++11 --compiler-options '-fPIC'
CXXFLAGS = -std=c++11
HLIB = libhello.a
SHLIB = libhello.so
CUDA_OBJECTS = bridge.o add.o
all: driver
%.o :: %.cu
nvcc -o $# $(NVCCFLAGS) -c -I. $<
%.o :: %.cpp
c++ $(CXXFLAGS) -o $# -c -I. $<
$(HLIB): $(CUDA_OBJECTS)
ar rcs $# $^
$(SHLIB): $(CUDA_OBJECTS)
nvcc $(NVCCFLAGS) --shared -o $# $^
#driver : driver.o $(HLIB)
# c++ -std=c++11 -fPIC -o $# driver.o -L. -lhello -L/usr/local/cuda-10.1/targets/x86_64-linux/lib -lcudart
driver : driver.o $(SHLIB)
c++ -std=c++11 -fPIC -o $# driver.o -L. -lhello
clean:
-rm -f driver *.o *.so *.a
Here are the various source files that the makefile takes as fodder.
add.cu:
__global__ void add(int n, int* a, int* b, int* c) {
int index = threadIdx.x;
int stride = blockDim.x;
for (int ii = index; ii < n; ii += stride) {
c[ii] = a[ii] + b[ii];
}
}
add.h:
extern __global__ void add(int n, int* a, int* b, int* c);
bridge.cu:
#include <iostream>
#include "add.h"
void bridge() {
int N = 1 << 16;
int blockSize = 256;
int numBlocks = (N + blockSize - 1)/blockSize;
int* a;
int* b;
int* c;
cudaMallocManaged(&a, N*sizeof(int));
cudaMallocManaged(&b, N*sizeof(int));
cudaMallocManaged(&c, N*sizeof(int));
for (int ii = 0; ii < N; ii++) {
a[ii] = ii;
b[ii] = 2*ii;
}
add<<<numBlocks, blockSize>>>(N, a, b, c);
cudaDeviceSynchronize();
for (int ii = 0; ii < N; ii++) {
std::cout << a[ii] << " + " << b[ii] << " = " << c[ii] << std::endl;
}
cudaFree(a);
cudaFree(b);
cudaFree(c);
}
bridge.h:
extern void bridge();
driver.cpp:
#include "bridge.h"
int main() {
bridge();
return 0;
}
I'm very new to cuda, so I expect that's where I'm doing something wrong. I've played a bit with using extern "C" declarations, but that just seems to move the "undefined symbol" error from run time to build time.
I'm familiar with various ways that one can end up with an undefined symbol, and I've mentioned various experiments I've already performed (static linking, extern "C" declarations) that make me think that this problem isn't addressed by the proposed duplicate question.
My unresolved symbol is _Z6bridgev
It looks to me as though the linker should be able resolve the symbol. If I can nm on driver.o, I see:
0000000000000000 T main
U _Z6bridgev
And if I run nm on libhello.so, I see:
0000000000006e56 T _Z6bridgev
When Robert Crovella was able to get my example to work on his machine, while I wasn't able to get his example to work on mine, I started realizing that my problem had nothing to do with cuda or nvcc. It was the fact that with a shared library, the loader has to resolve symbols at runtime, and my shared library wasn't in a "well-known location". I built a simple test case just now, purely with c++ sources, and repeated my failure. Once I copied libhello.so to /usr/local/lib, I was able to run driver successfully. So, I'm OK with closing my original question, if that's the will of the people.
So ive looked at similar issues and I followed what they said. I have made sure that my .h and .cpp file are in my main test file.
So I'm not really sure whats wrong. I fixed an earlier error like this but it was something I caught. Id appreciate some help.
Matrix-Multiply.h
Matrix-Multiply.h
#ifndef __MATRIX_MULTIPLY_H
#define __MATRIX_MULTIPLY_H
#include<stdio.h>
#include<stdlib.h>
float *expectedFinalMatrixOutput(int mA,int nA,int mB,int nB,float *matA,float *matB);
int readFiles(const char *matAFile,const char *matBFile);
#endif //__MATRIX_MULTIPLY_H
Matrix-Multiply.cpp
//.cpp file
#include<stdio.h>
#include<stdlib.h>
float *expectedFinalMatrixOutput(int mA,int nA,int mB,int nB,float *matA,float *matB)
{
int m = mA;
int n = nB;
int size = m * n;
float *finalMatrix[size];
//build both matrices
//for both the matA Column and matB Row need to be the
//same before even multiplying
//dot product matrix
//the end matrix needs the have the same number of rows as
//matA and same number of columns as matB
return *finalMatrix;
}
int readFiles(const char *matAFile,const char *matBFile)
{
int flag;
//read in file for matrixs
//set flag for whether true or false
//verify row and column being taken have actual values and
//that the sized are correct
return flag;
}
Matrix-Multiply_unittests.cpp
// tests.cpp
#include "Matrix-Multiply.h"
#include "Matrix-Multiply.cpp"
#include<gtest/gtest.h>
#include<stdio.h>
TEST(matrixSize,emptyMatrix)
{
float *matA = NULL;
float *matB = NULL;
float *matrix =
expectedFinalMatrixOutput(0,0,0,0,matA,matB);
ASSERT_EQ(*(matrix),0);
}
TEST(dotTest,oneByoneMatrix)
{
float fMatrix[1] = {1};
float sMatrix[1] = {1};
float *matrix = expectedFinalMatrixOutput(1,1,1,1,fMatrix,sMatrix);
ASSERT_EQ(matrix[0],1);
}
TEST(dotTest,twoBytwoMatrix)
{
float fMatrix[4] = {1,1,1,1};
float sMatrix[4] = {1,1,1,1};
float *matrix = expectedFinalMatrixOutput(2,2,2,2,fMatrix,sMatrix);
for(int i =0;i<4;i++)
{
EXPECT_EQ(2,matrix[i]);
}
}
TEST(ReadFilesTest,filesExist)
{
const char *matA = "../src/Matrix1_3_3.txt";
const char *matB = "../src/Matrix2_3_3.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
TEST(ReadFilesTest,filesDontExist)
{
const char *matA = "../src/notReal.txt";
const char *matB = "../src/Matrix2_3_3.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
TEST(ReadFilesTest,matrixSizeNotCompatible)
{
const char *matA = "../src/Matrix1_3_3.txt";
const char *matB = "../src/Matrix2_2_2.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
int main(int argc,char **argv)
{
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
Sorry if its not all on one line. I tried to get it that way. But the error i get is:
obj/Matrix-Multiply.o: In function `expectedFinalMatrixOutput(int, int, int,
int, float*, float*)':
Matrix-Multiply.cpp:(.text+0x0): multiple definition of
`expectedFinalMatrixOutput(int, int, int, int, float*, float*)'
/tmp/ccUgZRUB.o:Matrix-Multiply_unittests.cpp:(.text+0x0): first defined
here
obj/Matrix-Multiply.o: In function `readFiles(char const*, char const*)':
Matrix-Multiply.cpp:(.text+0xbe): multiple definition of `readFiles(char
const*, char const*)'
/tmp/ccUgZRUB.o:Matrix-Multiply_unittests.cpp:(.text+0xbe): first defined
here
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
Im using googletest and already make a call to make gtest
However this error occurs when i call make test.
Any help is appreciated
Makefile
CPP=g++
OBJ=obj
SRC=src
BIN=bin
CPPFLAGS=-I$(SRC)
GTEST_DIR=../googletest/googletest
gtest:
mkdir -p $(OBJ)
${CPP} -I${GTEST_DIR}/include -I${GTEST_DIR} \
-pthread -c ${GTEST_DIR}/src/gtest-all.cc -o $(OBJ)/gtest-all.o
ar -rv $(OBJ)/libgtest.a $(OBJ)/gtest-all.o
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
$(OBJ)/gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I${GTEST_DIR}/include -I$(GTEST_DIR) $(CXXFLAGS) -c
\
$(GTEST_DIR)/src/gtest_main.cc -o $#
$(OBJ)/gtest_main.a : $(OBJ)/gtest-all.o $(OBJ)/gtest_main.o
$(AR) $(ARFLAGS) $# $^
$(OBJ)/%.o: $(SRC)/%.cpp
$(CPP) $(CPPFLAGS) $(CXXFLAGS) -c -o $# $<
.PHONY: test clean
test: $(OBJ)/Matrix-Multiply.o $(OBJ)/gtest_main.a
mkdir -p $(BIN)
$(CPP) -I${GTEST_DIR}/include $(SRC)/Matrix-Multiply_unittests.cpp \
$(OBJ)/gtest_main.a $(OBJ)/Matrix-Multiply.o -o $(BIN)/Matrix-
Multiply_unittests -pthread
$(BIN)/Matrix-Multiply_unittests
clean:
rm -f $(BIN)/*
rm -f $(OBJ)/*
I think by #include "Matrix-Multiply.cpp" you are including the functions once.
Also (although not shown), you are linking with Matrix-Multiply on the link line.
You should not normally include a .cpp file. They are better bound by linking the file.
g++ -o my_awesome_app main.o Matrix-Multiply.o otherfile.o
Alright, new user here, and I've got a problem. I'm a new c++ student, and I have no prior experience in this language (before about 3 months ago). My assignment is as follows:
Write a program that declares an array darray of 50 elements of type double. Initialize the array so that the first 25 elements are equal to the square of the index variable, and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.
The program should have two functions: a function, initArray(), which initializes the array elements, and a function, prArray(), which prints the elements.
I have that, it's as follows
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
double initArray();
double prArray(double arrayone[50]);
double * dArray;
int main() {
dArray[50] = initArray();
system("PAUSE");
prArray(dArray);
system("PAUSE");
return 0;
}
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <iostream>
#include <string>
using namespace std;
double prArray(double arraytwo[50])
{
for (int x = 0; x < 50; x++) {
cout << arraytwo[x];
if (x = 9 || 19 || 29 || 39 || 49) {
cout << endl;
}
}
return 0;
}
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <iostream>
#include <string>
int x = 0;
double arrayone[50];
double initArray()
{
for (x = 0; x < 25; x++) {
arrayone[x] = (x*x);
}
for (x = 25; x <= 50; x++) {
arrayone[x] = (x * 3);
}
return arrayone[50];
}
Now my problem is that the assignment goes on to say
Write a Makefile to compile the program above that minimizes recompiling items upon changes. (e.g., if one function file gets updated, only the necessary file(s) are recompiled.) Include a clean target that removes compiled objects if invoked.
I have a basic makefile:
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=Main.cpp initializeArray.cpp printArray.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=Main
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $< -o $#
Now, what I need help with is turning this into a makefile that satisfies the assignment conditions - preferably with step-by-step instructions so that I can learn from this.
Modified your Makefile to:
Automatically generate header dependencies.
Re-build and re-link when Makefile changes.
CXX := g++
LD := ${CXX}
CXXFLAGS := -Wall -Wextra -std=gnu++14 -pthread
LDFLAGS := -pthread
exes :=
# Specify EXEs here begin.
Main.SOURCES := Main.cpp initializeArray.cpp printArray.cpp
exes += Main
# Specify EXEs here end.
all: $(exes)
.SECONDEXPANSION:
get_objects = $(patsubst %.cpp,%.o,${${1}.SOURCES})
get_deps = $(patsubst %.cpp,%.d,${${1}.SOURCES})
# Links executables.
${exes} : % : $$(call get_objects,$$*) Makefile
${LD} -o $# $(filter-out Makefile,$^) ${LDFLAGS}
# Compiles C++ and generates dependencies.
%.o : %.cpp Makefile
${CXX} -o $# -c ${CPPFLAGS} ${CXXFLAGS} -MP -MD $<
# Include the dependencies generated on a previous build.
-include $(foreach exe,${exes},$(call get_deps,${exe}))
.PHONY: all
I'm trying to build these files but it's giving me a multiple definition error.
main.cpp:
#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"
int main(int argc, char** args)
{
if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
{
SDL_Quit();
}
while(core.desiredstate != core.quit)
{
::currentstate->EventHandling();
::currentstate->Logic();
core.ChangeState();
::currentstate->Render();
::currentstate->Update();
}
SDL_FreeSurface(core.screen);
SDL_Quit();
}
Core.cpp:
#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>
/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS)
{
//starts SDL subsystems, returns false upon error
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//The screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
//Returns false if there was an error
if(screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Game", NULL);
return true;
}
/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
//original loaded image
SDL_Surface* original = SDL_LoadBMP(filename.c_str());
SDL_Surface* optimized = NULL;
if(original != NULL)
{
//Sets optimized to optimized version of original
optimized = SDL_DisplayFormat(original);
SDL_FreeSurface(original);
}
return optimized;
}
/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//holds the x y coordinates
SDL_Rect location;
location.x = x;
location.y = y;
if(destination != NULL)
{
SDL_BlitSurface(source, NULL, destination, &location);
}
}
/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate)
{
if(desiredstate != state_null && desiredstate != quit)
{
desiredstate = newstate;
}
}
/* Changes the game state */
void Core::ChangeState()
{
if(desiredstate != state_null && desiredstate != quit)
{
//frees old state memory
delete ::currentstate;
switch(desiredstate)
{
case intro:
//allocates new state memory
::currentstate = new Intro();
break;
}
stateID = desiredstate;
desiredstate = state_null;
}
}
GameStates.h:
#ifndef GAMESTATES_H
#define GAMESTATES_H
class GameStates
{
public:
virtual void EventHandling() = 0;
virtual void Logic() = 0;
virtual void Render() = 0;
virtual void Update() = 0;
};
#endif
Intro.h:
#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"
class Intro : public GameStates
{
private:
SDL_Surface* test;
public:
Intro();
void EventHandling();
void Logic();
void Render();
void Update();
~Intro();
} intro;
#endif
Intro.cpp:
#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"
Intro::Intro()
{
test = core.Load("test.bmp");
}
void Intro::EventHandling()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
core.SetState(core.quit);
break;
}
}
}
void Intro::Logic()
{
//to be coded when the program actually builds...
}
void Intro::Render()
{
core.ApplySurface(30, 30, test, core.screen);
}
void Intro::Update()
{
SDL_Flip(core.screen);
}
Intro::~Intro()
{
SDL_FreeSurface(test);
}
globals.h:
#include "GameStates.h"
#include "SDL/SDL.h"
GameStates* currentstate = NULL;
Sorry if the indentation is off; having to put four spaces for it to be seen as a code block messed with it a bit.
Heres the error message:
/tmp/ccWxKsO5.o:(.bss+0x0): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
/tmp/ccWxKsO5.o:(.bss+0x20): multiple definition of `currentstate'
/tmp/cc13Eqmt.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x0): multiple definition of `intro'
/tmp/ccWxKsO5.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x10): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Makefile:
OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h
$(CC) $(CFLAGS) main.cpp $(LIBS)
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
$(CC) $(CFLAGS) Core.cpp $(LIBS)
Intro.o : Intro.cpp GameStates.h Core.h
$(CC) $(CFLAGS) Intro.cpp $(LIBS)
The problem isn't your code, it's your build system.
Any sane build system matches the name of the object files to the name of the source files. But you have ccWxKsO5.o and cc13Eqmt.o. What's worse, the build system appears to be trying to link multiple objects generated from the same source (perhaps some were created by an earlier run of the compiler).
tempnam and globbing *.o is not a reasonable way to build C++ programs.
Well, there may be some code problems also. But those will be a thousand times easier to find and fix once the object names in the error messages correlate to source files.
For objects shared between multiple translation units, the rule is: there must be exactly one definition, but you may have multiple declarations.
In practice, this means: put "extern Class object;" in your .h file, and "Class object;" in exactly one of your .CPP files.
For intro, for example, change your Intro.h to:
class Intro : public GameStates
{
... // whatever
};
extern Intro intro;
and add this line to Intro.cpp:
Intro intro;
Similarly for currentstate, in globals.h:
extern GameStates* currentstate;
and in one .CPP (it doesn't matter to the compiler which one):
GateStates* currentstate = NULL;
P.s. Your makefile is broken. You pass -c, which means "don't link" to your link step. Try this:
OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h
$(CC) -c $(CFLAGS) main.cpp
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
$(CC) -c $(CFLAGS) Core.cpp
Intro.o : Intro.cpp GameStates.h Core.h
$(CC) -c $(CFLAGS) Intro.cpp
In globals.h, you must declare currentstate extern. Then create globals.cpp, with the definition (GameStates* currentstate = NULL;). I couldn't find any reference for intro or core in your code, but it's probably the same problem: you can declare global variables as often as you want as long as you declare them extern, and only define them once per resulting binary, in only one translation unit.
Also, you probably want to add a header guard (#ifndef GLOBALS_H ...) to globals.h, just in case you add anything else in there.
put include guards in globals.h
make GameStates* declaration extern
//globals.h
#ifndef _MY_GLOBALS_H_
#define _MY_GLOBALS_H_
#include "GameStates.h"
#include "SDL/SDL.h"
extern GameStates* currentstate;
#endif
//Main.cpp
#include "globals.h"
GameStates* currentState = 0;