Compilation issues in OpenGL code with g++ [duplicate] - c++

This question already has answers here:
'glCreateShader' was not declared in this scope?
(3 answers)
Closed 8 years ago.
When I try compiling the opengl Code written in c language with gcc with the following commands it runs fine :
gcc -Wall tutorial10.c -lGL -lglut -lGLU
But when I try doing the same compiling it by using g++
g++ -Wall tutorial10.c -lGL -lglut -lGLU
It starts to give a lot of errors like this :
tutorial10.c: In function ‘void drawRect()’:
tutorial10.c:28:34: error: ‘glClearBufferfv’ was not declared in this scope
tutorial10.c:34:28: error: ‘glUseProgram’ was not declared in this scope
tutorial10.c:36:24: error: ‘glGenBuffers’ was not declared in this scope
tutorial10.c:37:37: error: ‘glBindBuffer’ was not declared in this scope
tutorial10.c:47:71: error: ‘glBufferData’ was not declared in this scope
tutorial10.c:49:52: error: ‘glVertexAttribPointer’ was not declared in this scope
tutorial10.c:50:29: error: ‘glEnableVertexAttribArray’ was not declared in this scope
tutorial10.c:52:60: error: ‘glGetUniformLocation’ was not declared in this scope
tutorial10.c:54:42: error: ‘glUniform4f’ was not declared in this scope
tutorial10.c:59:30: error: ‘glDisableVertexAttribArray’ was not declared in this scope
tutorial10.c: In function ‘int main(int, char**)’:
tutorial10.c:93:34: error: ‘glCreateProgram’ was not declared in this scope
tutorial10.c:95:54: error: ‘glCreateShader’ was not declared in this scope
tutorial10.c:124:76: error: ‘glShaderSource’ was not declared in this scope
tutorial10.c:128:36: error: ‘glCompileShader’ was not declared in this scope
tutorial10.c:134:49: error: ‘glAttachShader’ was not declared in this scope
tutorial10.c:136:29: error: ‘glLinkProgram’ was not declared in this scope
tutorial10.c:147:35: error: ‘glDeleteShader’ was not declared in this scope
Headers from comment:
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
#include<malloc.h>
#include<string.h>
#include<math.h>

You don't have opengl functions declarations within your translation unit (most likely, you haven't included <GL/gl.h>).
gcc takes that because older versions of C language allows implicit declarations - whenever you use function, gcc automatically declares it with generic rules. It does NOT mean functions will be called properly - implicit rules are very generic and will cause problems on e.g. float argument types, so expect a lot of bugs here.
You can see warnings about implicit declarations if you add -Wimplicit-function-declaration (or -Wall, which includes this, along with many others) to your cflags.
g++, on other side, enabled C++ mode, which forbids implicit declarations.
To sum it all, don't use implicit declarations unless you truly understand what they do (but in that case you wouldn't want to use them anyway).

Related

Making an Arduino library independent. (c/c++)

I'm currently working on a project which executes correctly using an arduino library (VirtualWire), yet would like to move away from Arduino and back into pure C/C++.
I could:
- rewrite the actual arduino functions
- rewrite the calls to arduino functions (change pinmode to DDRx, etc)
- use the arduino core source code in conjunction with the library.
I have already attempted the last point, moving the relevent arduino source code in with the virtual wire library, however when i try to compile- i cant link against the VirtualWire library.
NOTE: the VirtualWire library and its dependencies (hopefully) is located in the same directory as my source code (as a test).
Using the following compile command, i achieve the following errors:
avr-g++ -mmcu=atmega32u4 -Os -DF_CPU=8000000UL -IVirtualWire -LVirtualWire -lVirtualWire transmitter.cpp -o transmitter.o
c:/winavr-20100110/bin/../lib/gcc/avr/4.3.3/../../../../avr/bin/ld.exe: cannot find -lVirtualWire
Or alternatively,
avr-g++ -mmcu=atmega32u4 -Os -DF_CPU=8000000UL -IVirtualWire -LVirtualWire transmitter.cpp -o transmitter.o
C:\cygwin\tmp/cc8d5ab7.o: In function `setup':
transmitter.cpp:(.text+0xe): undefined reference to `vw_set_tx_pin'
transmitter.cpp:(.text+0x14): undefined reference to `vw_set_rx_pin'
transmitter.cpp:(.text+0x1a): undefined reference to `vw_set_ptt_pin'
transmitter.cpp:(.text+0x20): undefined reference to `vw_set_ptt_inverted'
transmitter.cpp:(.text+0x28): undefined reference to `vw_setup'
C:\cygwin\tmp/cc8d5ab7.o: In function `main':
transmitter.cpp:(.text+0x6e): undefined reference to `vw_send'
transmitter.cpp:(.text+0x72): undefined reference to `vw_wait_tx'
transmitter.cpp:(.text+0x80): undefined reference to `delay'
And lastly,
avr-g++ -mmcu=atmega32u4 -Os -DF_CPU=8000000UL transmitter.cpp -o transmitter.o
transmitter.cpp:1:25: error: VirtualWire.h: No such file or directory
transmitter.cpp: In function 'int main()':
transmitter.cpp:26: error: 'HIGH' was not declared in this scope
transmitter.cpp:26: error: 'digitalWrite' was not declared in this scope
transmitter.cpp:27: error: 'uint8_t' was not declared in this scope
transmitter.cpp:27: error: expected primary-expression before ')' token
transmitter.cpp:27: error: 'vw_send' was not declared in this scope
transmitter.cpp:28: error: 'vw_wait_tx' was not declared in this scope
transmitter.cpp:29: error: 'LOW' was not declared in this scope
transmitter.cpp:30: error: 'delay' was not declared in this scope
transmitter.cpp: In function 'void setup()':
transmitter.cpp:42: error: 'CLKPR' was not declared in this scope
transmitter.cpp:44: error: 'vw_set_tx_pin' was not declared in this scope
transmitter.cpp:45: error: 'vw_set_rx_pin' was not declared in this scope
transmitter.cpp:46: error: 'vw_set_ptt_pin' was not declared in this scope
transmitter.cpp:47: error: 'vw_set_ptt_inverted' was not declared in this scope
transmitter.cpp:48: error: 'vw_setup' was not declared in this scope
transmitter.cpp:49: error: 'OUTPUT' was not declared in this scope
transmitter.cpp:49: error: 'pinMode' was not declared in this scope
I am familiar with C/C++, however only today did i find out there were different types of libraries.
Is there something obvious i am doing wrong, or an easier way to transfer this library from Arduino to pure C/C++?
Thanks in advance!

par2tbb fails to compile with an "auto_ptr is not a member of std" error

I am trying to compile par2tbb on Gentoo Linux kernel 3.18.12, and I keep getting the following compilation error:
par2cmdline.cpp: In function ‘int main(int, char**)’:
par2cmdline.cpp:88:3: error: ‘auto_ptr’ is not a member of ‘std’
std::auto_ptr<CommandLine> commandline(new CommandLine);
^
I already checked that #include <memory.h> was present on the source code, I already added CXXFLAGS = -std=c++11 to the makefile, and it still won't compile.
Looking up online, these two measures are the only fixes I could find. Any idea on what's going on?

OpenGL errors finding certain libraries

I am learning about OpenGL and am having great difficulty opening a file with openGL. This is what my reference says to do:
"Compile and run the program
To compile the sample program, you need to link to the OpenGL and freeglut libraries. You will also need the GLEW library to be able to use many OpenGL functions, including shaders.
The exact names of these libraries vary from system to system, for example:
Linux
: libGL.a, libglut.a
Compile with
g++ main.cpp -lGL -lglut -lGLE"
I have run this code in my linux terminal (I'm using Redhat) and I get a whole bunch of errors about not being able to locate the files
[a1649446#ingb24w027 code]$ g++ main.cpp -lGL -lglut -lGLEW
main.cpp:46:23: error: glm/glm.hpp: No such file or directory
main.cpp:47:40: error: glm/gtc/matrix_transform.hpp: No such file or directory
main.cpp:48:32: error: glm/gtc/type_ptr.hpp: No such file or directory
main.cpp:62: error: ‘glm’ has not been declared
main.cpp:62: error: expected constructor, destructor, or type conversion before ‘cameraMatrix’
main.cpp: In function ‘int setShaderData(const unsigned int&)’:
main.cpp:102: error: ‘glm’ has not been declared
main.cpp:102: error: expected ‘;’ before ‘projection’
main.cpp:105: error: ‘glm’ has not been declared
main.cpp:105: error: ‘projection’ was not declared in this scope
main.cpp:109: error: ‘glm’ has not been declared
main.cpp:109: error: expected ‘;’ before ‘modelview’
main.cpp: In function ‘void render()’:
main.cpp:187: error: ‘cameraMatrix’ was not declared in this scope
main.cpp:187: error: ‘glm’ has not been declared
main.cpp:200: error: ‘glm’ has not been declared
I'm sure the answer is painfully obvious, but I am an OpenGL noob. I have even located all the openGL libraries and copy-pasted them into the folder with my main.cpp and all associated files, to no avail.

Errors compiling wxWidgets

I am trying to run the sample code shown here. I built wxWidgets from source using MinGW, with this command: mingw32-make -f makefile.gcc SHARED=1 UNICODE=0 BUILD=debug. In my Eclipse settings, I add -Iwx288/include, -Lwx288/lib, -lwxbase28d_gcc_custom and -lwxmsw28d_core_gcc_custom. I get very very confused, due to the number of ways to compile and inexperience with linkers. I get these errors, what am I doing wrong? I'm not even citing wxMutex in my program.
23:15:19 **** Incremental Build of configuration Debug for project A ****
Info: Internal Builder is used for build
g++ "-IC:\\Files\\Programming\\C++\\A\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "include\\wx\\thrimpl.o" "..\\include\\wx\\thrimpl.cpp"
..\include\wx\thrimpl.cpp:18:1: error: 'wxMutex' does not name a type
..\include\wx\thrimpl.cpp:29:1: error: 'wxMutex' does not name a type
..\include\wx\thrimpl.cpp:34:6: error: 'wxMutex' has not been declared
..\include\wx\thrimpl.cpp:34:22: error: non-member function 'bool IsOk()' cannot have cv-qualifier
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:36:12: error: 'm_internal' was not declared in this scope
..\include\wx\thrimpl.cpp:36:26: error: 'NULL' was not declared in this scope
..\include\wx\thrimpl.cpp: At global scope:
..\include\wx\thrimpl.cpp:39:1: error: 'wxMutexError' does not name a type
..\include\wx\thrimpl.cpp:47:1: error: 'wxMutexError' does not name a type
..\include\wx\thrimpl.cpp:55:1: error: 'wxMutexError' does not name a type
..\include\wx\thrimpl.cpp:224:1: error: 'wxCondition' does not name a type
..\include\wx\thrimpl.cpp:235:1: error: 'wxCondition' does not name a type
..\include\wx\thrimpl.cpp:240:6: error: 'wxCondition' has not been declared
..\include\wx\thrimpl.cpp:240:26: error: non-member function 'bool IsOk()' cannot have cv-qualifier
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:240:6: error: redefinition of 'bool IsOk()'
..\include\wx\thrimpl.cpp:34:6: error: 'bool IsOk()' previously defined here
..\include\wx\thrimpl.cpp:242:12: error: 'm_internal' was not declared in this scope
..\include\wx\thrimpl.cpp:242:26: error: 'NULL' was not declared in this scope
..\include\wx\thrimpl.cpp: At global scope:
..\include\wx\thrimpl.cpp:245:1: error: 'wxCondError' does not name a type
..\include\wx\thrimpl.cpp:253:1: error: 'wxCondError' does not name a type
..\include\wx\thrimpl.cpp:261:1: error: 'wxCondError' does not name a type
..\include\wx\thrimpl.cpp:269:1: error: 'wxCondError' does not name a type
..\include\wx\thrimpl.cpp:281:1: error: 'wxSemaphore' does not name a type
..\include\wx\thrimpl.cpp:291:1: error: 'wxSemaphore' does not name a type
..\include\wx\thrimpl.cpp:296:6: error: 'wxSemaphore' has not been declared
..\include\wx\thrimpl.cpp:296:26: error: non-member function 'bool IsOk()' cannot have cv-qualifier
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:296:6: error: redefinition of 'bool IsOk()'
..\include\wx\thrimpl.cpp:34:6: error: 'bool IsOk()' previously defined here
..\include\wx\thrimpl.cpp:298:12: error: 'm_internal' was not declared in this scope
..\include\wx\thrimpl.cpp:298:26: error: 'NULL' was not declared in this scope
..\include\wx\thrimpl.cpp: At global scope:
..\include\wx\thrimpl.cpp:301:1: error: 'wxSemaError' does not name a type
..\include\wx\thrimpl.cpp:309:1: error: 'wxSemaError' does not name a type
..\include\wx\thrimpl.cpp:317:1: error: 'wxSemaError' does not name a type
..\include\wx\thrimpl.cpp:325:1: error: 'wxSemaError' does not name a type
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:299:1: warning: control reaches end of non-void function [-Wreturn-type]
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:243:1: warning: control reaches end of non-void function [-Wreturn-type]
..\include\wx\thrimpl.cpp: In function 'bool IsOk()':
..\include\wx\thrimpl.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
23:15:20 Build Finished (took 657ms)
Why are you trying to compile include/wx/thrimpl.cpp? It's not your file, it's part of wxWidgets and it shouldn't be compiled as part of your project. Just remove it from the list of project files.
I'm guessing you are either missing something in your include path or some of the Defines required by wxWidgets in order to process wxWidgets Header-Files correctly
WX comes with a tool called wx-config ( should be located somewhere in wxWidgets/bin if your build was successful)
wx-config is a shell-script, you can execute it using mingw-msys
wx-config --cxxflags produces all Include Paths and Defines needed to compile object files.
wx-config --libs takes care of everything needed to link the program
I would recommend using wx-config in your makefile/builder/....
or check the output produced by it and compare it to your build process
Samples form my current installation:
$ wx-config --cxxflags --static=no --debug=yes --unicode=no -I/wx/lib/wx/include/msw-ansi-debug-2.8 -I/wx/include/wx-2.8 -D_WXDEBUG_ -DWXUSINGDLL -D_WXMSW_ -mthreads
$ ./wx-config --libs --static=no --debug=yes --unicode=no -L/wx/lib -mthreads -Wl,--subsystem,windows -mwindows -lwx_mswd_richtext-2.8 -lwx_mswd_aui-2.8 -lwx_mswd_xrc-2.8 -lwx_mswd_qa-2.8 -lwx_mswd_html-2.8 -lwx_mswd_adv-2.8 -lwx_mswd_core-2.8 -lwx_based_xml-2.8 -lwx_based_net-2.8 -lwx_based-2.8

C++ Compilation error against GNU's Multi-precision library

I get the following error when compiling this third-party library (called azove 2.0) which relies on the GNU Multi-precision library:
> make
g++ -Wall -O3 -DFIX_NUMBER_OF_SIGMDDNODE_SONS -DUSE_TIMER -I. -c conBDD.cpp -o conBDD.o
In file included from conBDDnode.hpp:27,
from conBDD.hpp:25,
from conBDD.cpp:22:
/usr/include/gmpxx.h: In destructor ‘__gmp_alloc_cstring::~__gmp_alloc_cstring()’:
/usr/include/gmpxx.h:2096: error: ‘strlen’ was not declared in this scope
conBDD.cpp: In member function ‘void conBDD::build()’:
conBDD.cpp:61: error: ‘numeric_limits’ was not declared in this scope
conBDD.cpp:61: error: expected primary-expression before ‘int’
conBDD.cpp:61: error: expected `;' before ‘int’
conBDD.cpp:68: error: expected primary-expression before ‘int’
conBDD.cpp:68: error: expected `;' before ‘int’
make: *** [conBDD.o] Error 1
I have tried adding either and both of the following lines
#include <cstdlib>
using std::strlen;
to conBDD.cpp, but the error persists.
I can't tell if this is an error comes from GNU's Multi-precision library or from Azove. Any pointers would be greatly appreciated.
I would start by apportioning blame. Create an empty cpp file, say test.cpp, in your project and include only the offending gmpxx.h file. If you can compile test cpp, GMP is off the hook. Then try including only the offending azove header. If you can compile the azove header in an otherwise empty file, azove is off the hook and something you are including/defining is interfering with them. Once you have narrowed down the source of the problem you should find it easier to fix.