I am quite new to linux development, and having trouble using a class from a separate file in my main. The error I am getting when making (after having created makefile with cmake) is that system does not name a type, I think the code in system class is correct as if I compile without attempting to create an object of the system class I have no errors, so I think it may be to do with the way I have written the CMakeLists.txt file.
This is my CMakeLists file:
cmake_minimum_required (VERSION 2.6)
project (GL_PROJECT)
add_library(system system.cpp)
include_directories(${GL_PROJECT_SOURCE_DIR})
link_directories(${GL_PROJECT_BINARY_DIR})
find_package(X11)
if(NOT X11_FOUND)
message(FATAL_ERROR "Failed to find X11")
endif(NOT X11_FOUND)
find_package(OpenGL)
if(NOT OPENGL_FOUND)
message(FATAL_ERROR "Failed to find opengl")
endif(NOT OPENGL_FOUND)
set(CORELIBS ${OPENGL_LIBRARY} ${X11_LIBRARY})
add_executable(mainEx main.cpp system.cpp)
target_link_libraries(mainEx ${CORELIBS} system)
I have in the source directory my main.cpp, system.h (class definition) and system.cpp (class implementation)
In main:
#include"system.h"
system sys;
int main(int argc, char *argv[]) {
while(1)
{
sys.Run();
}
}
X11 and GL includes etc are in system.h, I think the code in there is correct and not causing the error (as it builds fine if i dont try and create an instance of the class). I have omitted the actual header and implementation for brevity in the hope that it will be an obvious error in the CMakeList file, but if necessary I can add these too?
Any ideas?
Thanks in advance.
EDIT: Here are the errors in terminal
[tims@localhost build]$ make
Scanning dependencies of target system
[ 33%] Building CXX object CMakeFiles/system.dir/system.cpp.o
Linking CXX static library libsystem.a
[ 33%] Built target system
Scanning dependencies of target mainEx
[ 66%] Building CXX object CMakeFiles/mainEx.dir/main.cpp.o
/home/tims/Code/GL_Project/main.cpp:5:1: error: ‘system’ does not name a type
/home/tims/Code/GL_Project/main.cpp: In function ‘int main(int, char**)’:
/home/tims/Code/GL_Project/main.cpp:11:3: error: ‘sys’ was not declared in this scope
make[2]: *** [CMakeFiles/mainEx.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/mainEx.dir/all] Error 2
make: *** [all] Error 2
Edit 2: system.h
#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
class system {
public:
system(void);
~system(void);
void CreateGLXWindow();
void Run();
//Contains information about X server we will be communicating with
Display *display;
XEvent xEvent;
//Window instance
Window rootWindow;
XVisualInfo *xVisInfo;
XSetWindowAttributes setWindAttrs;
XWindowAttributes xWindAttrs;
//GL
GLXContext context;
Colormap cmap;
Window window;
private:
};
The class name
systemclashes with theint system(const char*)function, declared instdlib.h, which is included by yoursystem.h. You either need to rename thesystemclass or move it into a namespace, because a class and a function can’t have the same name in C++.