I'm trying to create a static shader library with OpenGL.
I want it to be as portable as possible (Windows Linus Android ...).
For this propose i made a header what includes gl and glext:
OpenGL.hpp
...
#define GL_GLEXT_PROTOTYPES
#if defined(OS_WINDOWS)
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glext.h>
#elif defined(OS_LINUX)
#include <GL/gl.h>
#include <GL/glext.h>
...
And Shader.hpp
...
class Shader
{
unsigned int m_id;
public:
Shader();
...
};
...
And Shader.cpp
...
#include <Shader.hpp>
#include <OpenGL.hpp>
Shader::Shader()
{
m_id = glCreateProgram();
...
}
...
Then complied Shader.cpp to Shader.o and with ar created libShader.a.
After that on android i can compile like that:
g++ -I ./include -c main.cpp -o main.o
g++ -L ./lib -o main main.o -lShader -lSDL2 -lGLESv1_CM -lGLESv2
And it runs flawless.
But on windows when i link
g++ -L ./lib -o main.exe main.o -lShader -lSDL2 -lglew32 -lopengl32 -lglu32
G++ gives lib\libShader.a(Shader.o):Shader.cpp undefined reference to glCreateProgram#8
error.
On windows main.cpp looks like this:
#include <GL/glew.h>
#include <Shader.hpp>
...
int main()
{
Shader shader1;
...
GLuint test_id_for_main = glCreateProgram();
...
}
...
The wierd thing is test_id_for_main works (if i comment out the Shader-s) without link error.
What did i do incorrectly on windows?
I ended up creating my own OpenGL headers and loading the functions with wgl/glx/dlopen
Related
I have problems with my Makefile.
I used the following structure to generate the .o files of each cpp file, but does not work (using c works without problems, I cant find what is the problem)
%.o : %.cpp %.h
g++ -c -Wall $< -o $#
And the error while compiling is a function is declared in a separated h and cpp file and added to the main file. But when I try to generate de .o file of main.cpp marks error in the function.
The command I used to compile the main.cpp -> g++ -c main.cpp -o main.o
The error that gives me is:
main.cpp: In function ‘int main(int, char)’:
main.cpp:9:9: error: ‘number’ was not declared in this scope9 | number();
This is the compiler that I used for it:
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Linux 5.15.0-40-generic
Please, anyone could explain me if I'm doing wrong of something is left
/*main.cpp*/
#include <iostream>
#include "numb.h"
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Run"<<endl;
number();
cout<<"end Run"<<endl;
return 0;
}
/*end main.cpp*/
/*numb.cpp*/
#include <iostream>
#include "numb.h"
using namespace std;
int number()
{
cout<<"Function"<<endl;
return 117;
}
/*end numb.cpp*/
/*numb.h*/
#include <iostream>
#define NUMB_H
#ifndef NUMB_H
int number();
#endif
/*end numb.h*/
You got the header guard in the wrong order.
Instead of:
#define NUMB_H
#ifndef NUMB_H
It is supposed to be:
#ifndef NUMB_H
#define NUMB_H
When compiling specify both CPP files, because #include fixes compile errors, but does not fix linker errors
g++ -c main.cpp numb.cpp ...
As a rule, in header files nothing have to be outside the #define guards:
/*numb.h*/
#define NUMB_H
#ifndef NUMB_H
#include <iostream>
I have a C++ constructor file (formatting_SQ.cpp) of a header file formatting_SQ.h which I want to link to other constructor files of header files (neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h) in order to have formatting_SQ.o.
Then, I want to link my main.cpp file with this formatting_SQ.o file. The problem is: formatting_SQ is embedded with python, and as far as my understanding goes, C++ embedded with Python needs the compiling flag -lpython3.6m on Linux: such flag requires a reference to a main() function, which I don't have in formatting_SQ.cpp because it's a constructor file meant to be an object file.
So I first tried to create object files for each constructor file and then link everything together at once:
g++ -c -O3 -Wall -fPIC -fopenmp -std=c++14 -lstdc++ `python3 -m pybind11 --includes` *.cpp
g++ -o the_executable neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o main.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m
Here comes my first question: Are these command right or is there eventually a compilation flag missing ? This gives me a segmentation fault as I try to execute ./the_executable.
Then, I tried to compile formatting_SQ.cpp independently with all other constructor files, but as expected, this doesn't work because there is no reference to main in formatting_SQ.cpp.
g++ -o temp_formatting neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m
So here comes my second question: how could I create a python embedded object file linking formatting_SQ.cpp with all other constructor files without having this undefined reference to main error ?
formatting_SQ.cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"
namespace py = pybind11;
py::module compile_data = py::module::import("initialize");
main.cpp
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include "formatting_SQ.h"
#include <omp.h>
namespace py = pybind11;
int main(int argc, char** argv){
....
So after some long hours of research I can conclude that the compilation method is correct but BE EXTREMELY CAREFULL with where you declare your import modules from python, because this was the problem for me
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"
namespace py = pybind11;
py::module compile_data = py::module::import("initialize"); DON'T DO THIS its wrong !!!
You must declare your modules locally otherwise there be some conflicts in the namespace as the same module may be imported more than once and this causes the segmentation fault.
I already searched and found a solution for this problem but i find this a little bit strange. Anyway my problem is this:
Personal.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
Personal.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
Now i want to compile in main
main.cpp
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
When I write: g++ -Wall main.cpp -o main it gives me :
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
The solution:
g++ -Wall Personal.cpp main.cpp -o main
Why do i need compile the Personal.cpp too?
Or the other main version is to include instead of "Personal.h", "Personal.cpp"
main.cpp
#include "Personal.cpp"
#include <iostream>
#include <vector>
Then the normal g++ -Wall main.cpp -o main works
Can someone help me?
Why do i need compile the Personal.cpp too?
Because you use functions that are defined in that file. In particular, you use the functions Personal::Personal and Personal::~Personal.
Can someone help me?
Make sure that all functions (that are odr-used) are defined in exactly one (or in all files, in case of inline functions) of the source files that you compile and link together.
I have run into a problem while using C and C++ code together. The 'make' command returns "Undefined reference to function" for all functions in SPConfig.c and SPLogger.c, when called from SPImageProc.cpp
#include sections of these relevant files are given below:
SPLogger.c
#include "SPLogger.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
SPConfig.h
#ifndef SPCONFIG_H_
#define SPCONFIG_H_
#include <stdbool.h>
#include <stdio.h>
#include "SPLogger.h"
//Functions definitions
#endif /* SPCONFIG_H_ */
SPConfig.c
#include "SPConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
SPImageProc.h
#ifndef SPIMAGEPROC_H_
#define SPIMAGEPROC_H_
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
extern "C" {
#include "SPConfig.h"
#include "SPPoint.h"
}
namespace sp {
//Class and function definitions
}
SPImageProc.cpp
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <cstdio>
#include "SPImageProc.h"
extern "C" {
#include "SPLogger.h"
}
Makefile
CC = gcc
CPP = g++
#put all your object files here
OBJS = main.o SPImageProc.o SPPoint.o
#The executabel filename
EXEC = SPCBIR
INCLUDEPATH=/usr/local/lib/opencv-3.1.0/include/
LIBPATH=/usr/local/lib/opencv-3.1.0/lib/
LIBS=-lopencv_xfeatures2d -lopencv_features2d \
-lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
CPP_COMP_FLAG = -std=c++11 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
C_COMP_FLAG = -std=c99 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
.PHONY: all clean
all: $(EXEC)
$(EXEC): $(OBJS)
$(CPP) $(OBJS) -L$(LIBPATH) $(LIBS) -o $#
main.o: main.cpp #put dependencies here!
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c++ source file
#use g++ -MM SPImageProc.cpp to see dependencies
SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c source file
#use "gcc -MM SPPoint.c" to see the dependencies
SPPoint.o: SPPoint.c SPPoint.h
$(CC) $(C_COMP_FLAG) -c $*.c
clean:
rm -f $(OBJS) $(EXEC)
Some of Makefile errors:
SPImageProc.o: In function `sp::ImageProc::initFromConfig(sp_config_t*)':
SPImageProc.cpp:(.text+0xc8): undefined reference to `spConfigGetPCADim'
SPImageProc.cpp:(.text+0xf2): undefined reference to `spLoggerPrintError'
SPImageProc.cpp:(.text+0x12c): undefined reference to `spConfigGetNumOfImages'
I have implemented the functions in their respective C and CPP files. I have tried a lot to fix it myself and looked it up on Stack Overflow for similar problems but couldn't find a solution. Please help.
You are not linking SPLogger.o and SPConfig.o
Or even compiling them for that matter.
You need to add make rules for SPLogger.o and SPConfig.o similar to SPImageProc.o and you need to add them to OBJS.
I have 2 cpp and 3 header files in my project. When I compile them in VS it works smoothly and I get no error message. But when I try to compile it on SSH network by this line:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
It says:
In function `_start':
(.text+0x20): undefined reference to `main'
Do not say "do not forget to write main function" because it is already there and my project works on VS. What to do then?
Here is related part from my codes. Program.cpp until main:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include "lineoperations.h"
using namespace std;
line bankline;
bool operate(char);
void search(char[]);
void add(char[]);
void removee(char[]);
void transaction();
void printline();
int main(){
bankline.create();
bool end = false;
while (!end) {
end = bankline.decideFunction();
}
bankline.close();
return EXIT_SUCCESS;
}
It goes on but it is not necessary to paste them I guess. If you need to see other cpp file or header files I'll paste them as well.
The command:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
tells g++ to compile and link the files:
lineoperations.cpp customer.h transaction.h lineoperations.h
and output an executable program called program.cpp.
This fails with the linkage error you have observed because
main is defined in program.cpp, which you are not compiling or linking.
Try this instead:
g++ -o program program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
or if you are on Windows:
g++ -o program.exe program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
And BTW, there is no need to list the header files on the commandline. They are included by
the source files, I presume.