g++ when compile and link multiple files - c++

I encounter g++ compile/link problem.
I want to implement a Dynamic Array.
I separated my class definition and implementation into "DArray.h" and "DArray.cpp" files.
And do test in "DArraytest.cpp" file within which contains a main functionn.
all there files are in same directory, and I use the following command to compile
g++ *.cpp
but it still gives me such link error
/tmp/cc4tu73o.o: In function __static_initialization_and_destruction_0(int, int)':
DArray.cpp:(.text+0x1d): undefined reference tostd::ios_base::Init::Init()'
DArray.cpp:(.text+0x34): undefined reference to std::ios_base::Init::~Init()'
/tmp/ccdHUZjJ.o: In functionmain':
DArraytest.cpp:(.text+0x12): undefined reference to DArray<int>::DArray()'
DArraytest.cpp:(.text+0x2e): undefined reference toDArray::append(int const&)'
DArraytest.cpp:(.text+0x4a): undefined reference to DArray<int>::append(int const&)'
DArraytest.cpp:(.text+0x66): undefined reference toDArray::append(int const&)'
DArraytest.cpp:(.text+0x82): undefined reference to DArray<int>::append(int const&)'
DArraytest.cpp:(.text+0xa3): undefined reference tostd::cout'
DArraytest.cpp:(.text+0xa8): undefined reference to std::ostream::operator<<(int)'
DArraytest.cpp:(.text+0xb0): undefined reference tostd::basic_ostream >& std::endl >(std::basic_ostream >&)'
DArraytest.cpp:(.text+0xb8): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
DArraytest.cpp:(.text+0xc9): undefined reference toDArray::~DArray()'
DArraytest.cpp:(.text+0xeb): undefined reference to DArray<int>::~DArray()'
/tmp/ccdHUZjJ.o: In function__static_initialization_and_destruction_0(int, int)':
DArraytest.cpp:(.text+0x11d): undefined reference to std::ios_base::Init::Init()'
DArraytest.cpp:(.text+0x134): undefined reference tostd::ios_base::Init::~Init()'
/tmp/ccdHUZjJ.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Can someone help me

I think you need to provide all the .cpp file names to g++. To avoid this you can create a makefile which can automatically apply same compilation rule to all .cpp files

Related

G++ undefined references for libuvc and opencv2

I'm trying to compile a C++ program which includes the libuvc.h header file and some more header files of opencv2.
The problem is, that I'm getting the following errors (undefinded references):
root#Raspi_DataHarvest:~/Schreibtisch# g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::start(int, CameraProperties, int)':
UVCCameraHandler.cpp:(.text+0x164): undefined reference to `uvc_init'
UVCCameraHandler.cpp:(.text+0x184): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x204): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0x334): undefined reference to `uvc_open'
UVCCameraHandler.cpp:(.text+0x354): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x3a8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x3e8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x40c): undefined reference to `uvc_print_diag'
UVCCameraHandler.cpp:(.text+0x424): undefined reference to `uvc_print_stream_ctrl'
UVCCameraHandler.cpp:(.text+0x43c): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x46c): undefined reference to `uvc_stream_open_ctrl'
UVCCameraHandler.cpp:(.text+0x48c): undefined reference to `uvc_stream_start_iso'
UVCCameraHandler.cpp:(.text+0x4a4): undefined reference to `uvc_perror'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesGrayscale(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x5c8): undefined reference to `uvc_stream_get_frame'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesColor(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x864): undefined reference to `uvc_stream_get_frame'
UVCCameraHandler.cpp:(.text+0x92c): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::uvcFindDevice(uvc_context*, uvc_device**, int, int, char const*, int)':
UVCCameraHandler.cpp:(.text+0xae0): undefined reference to `uvc_get_device_list'
UVCCameraHandler.cpp:(.text+0xb10): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0xd0c): undefined reference to `uvc_get_device_descriptor'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::setProperties(CameraProperties)':
UVCCameraHandler.cpp:(.text+0xfb0): undefined reference to `uvc_get_gain'
UVCCameraHandler.cpp:(.text+0xff8): undefined reference to `uvc_set_gain'
UVCCameraHandler.cpp:(.text+0x103c): undefined reference to `uvc_set_ae_mode'
UVCCameraHandler.cpp:(.text+0x1050): undefined reference to `uvc_set_exposure_abs'
UVCCameraHandler.cpp:(.text+0x1068): undefined reference to `uvc_set_ae_mode'
/tmp/ccvQZdkd.o: In function `cv::String::String(char const*)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x60): undefined reference to `cv::String::allocate(unsigned int)'
/tmp/ccvQZdkd.o: In function `cv::String::~String()':
UVCCameraHandler.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::String::operator=(cv::String const&)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x30): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned int)':
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x140): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x244): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::~Mat()':
UVCCameraHandler.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator=(cv::Mat const&)':
UVCCameraHandler.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x140): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccvQZdkd.o: In function `cv::Mat::create(int, int, int)':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0xc4): undefined reference to `cv::Mat::create(int, int const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::release()':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator()(cv::Rect_<int> const&) const':
UVCCameraHandler.cpp:(.text._ZNK2cv3MatclERKNS_5Rect_IiEE[_ZNK2cv3MatclERKNS_5Rect_IiEE]+0x24): undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)'
collect2: error: ld returned 1 exit status
So, this looks to me like g++ does not find the location of the header files, but they are installed in the correct directory!
root#Raspi_DataHarvest:~# find / | grep libuvc.h
/usr/local/include/libuvc/libuvc.h
root#Raspi_DataHarvest:~# find / | grep opencv2/opencv.hpp
/usr/local/include/opencv2/opencv.hpp
Furthermore, there's no effect using the absolute paths, like:
#include "/usr/local/include/libuvc.h"
...
Does anyone know how to solve the reference errors?
Try
g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -luvc -lopencv
The most preferable
g++ UVCCameraHandler.cpp `pkg-config --libs --cflags opencv` `pkg-config --libs --cflags lubuvc`
Building a C++ program is a three-step process:
Editing the source
Compiling the source files into object files
Linking the object files into the final executable
Modern compilers often use front-end programs (like e.g. GCC with the g++ program) that combine steps 2 and 3 into a single action.
In step 2 above, the header files are included.
In step 3 you need to link with the actual libraries. You do not do this, so the linker will complain that it can't find the functions you use.
Linking with the libraries using GCC is done with the -l (lower-case L) option, listing the library name. Like for example -lopencv to link with the OpenCV library.
If the libraries are in a non-standard location (which /usr/local/lib usually is) then you need to tell the linker where to find the libraries as well. This is done with the -L option, similar to the -I option you already use to tell the compiler where to search for header-files. For example -L/usr/local/lib.
You need to link against the correct libraries (assuming your libs are in /usr/local/lib):
g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -L/usr/local/lib -luvc -lopencv_core

compile ExcelFormat Library

Could someone tell me how to solve this issue. I'm trying to compile this library here:
http://www.codeproject.com/Articles/42504/ExcelFormat-Library
I'm doing: g++ Examples.cpp
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
If my question is to easy/basic/simple/os dependent... please tell recommend me a book or two that I should read first in order to be able to figure out an answer for this question.
Here's the error message:
/tmp/cczceVBy.o: In function example1(char const*)':
Examples.cpp:(.text+0x2d): undefined reference toYExcel::BasicExcel::BasicExcel()'
Examples.cpp:(.text+0x41): undefined reference to YExcel::BasicExcel::New(int)'
Examples.cpp:(.text+0x55): undefined reference toYExcel::BasicExcel::GetWorksheet(int)'
Examples.cpp:(.text+0x75): undefined reference to ExcelFormat::XLSFormatManager::XLSFormatManager(YExcel::BasicExcel&)'
Examples.cpp:(.text+0xf2): undefined reference toYExcel::BasicExcelWorksheet::Cell(int, int)'
Examples.cpp:(.text+0x10d): undefined reference to YExcel::BasicExcelCell::Set(char const*)'
Examples.cpp:(.text+0x166): undefined reference toYExcel::BasicExcelWorksheet::Cell(int, int)'
Examples.cpp:(.text+0x173): undefined reference to YExcel::BasicExcelCell::Set(char const*)'
Examples.cpp:(.text+0x287): undefined reference toYExcel::BasicExcelWorksheet::Cell(int, int)'
Examples.cpp:(.text+0x2a2): undefined reference to YExcel::BasicExcelCell::Set(char const*)'
Examples.cpp:(.text+0x2df): undefined reference toYExcel::BasicExcelWorksheet::Cell(int, int)'
---- snip many more of the same kind -----
collect2: ld returned 1 exit status
Use BasicExcel.Cpp and BasicExcel.hpp file in your project directory which comes with the Excelformat zip file.

Undefined reference in g++ linking

Every time I make a new module and try to link it to my main class (Estudiant.o) using this line:
g++ -o red1.exe red1.o %OBJETOS_CPP%\Estudiant.o
I get this error:
red1.o:red1.cpp:(.text+0xd): undefined reference to
Estudiant::consultar_DNI() const' red1.o:red1.cpp:(.text+0x18):
undefined reference toEstudiant::Estudiant(int)'
red1.o:red1.cpp:(.text+0x25): undefined reference to
Estudiant::consultar_nota() const' red1.o:red1.cpp:(.text+0x74):
undefined reference toEstudiant::afegir_nota(double)'
red1.o:red1.cpp:(.text+0x83): undefined reference to
Estudiant::~Estudiant()' red1.o:red1.cpp:(.text+0xa3): undefined
reference toEstudiant::consultar_nota() const'
red1.o:red1.cpp:(.text+0xe5): undefined reference to
Estudiant::modificar_nota(double)' red1.o:red1.cpp:(.text+0x10b):
undefined reference toEstudiant::Estudiant()'
red1.o:red1.cpp:(.text+0x13a): undefined reference to
Estudiant::llegir_estudiant()' red1.o:red1.cpp:(.text+0x144):
undefined reference toEstudiant::te_nota() const'
red1.o:red1.cpp:(.text+0x182): undefined reference to
Estudiant::escriure_estudiant() const' red1.o:red1.cpp:(.text+0x18c):
undefined reference toEstudiant::~Estudiant()'
red1.o:red1.cpp:(.text+0x19f): undefined reference to
Estudiant::~Estudiant()'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe:
red1.o: bad reloc address 0x0 in section.ctors' collect2.exe: error:
ld returned 1 exit status
And I don't have the original Estudiant.cpp so I thought it might be because different compilers were used for compiling and for the link, but I reinstalled MinGW and I'm still getting this error.
I also tried to replace all the files and it didn't work.
I managed to get the source code of the Estudiant.o and compiled another version of it using the code and it worked. I don't know why the same code was compiled on two different machines and one worked and the other one didn't.

Ogre g++ compile-time error

On ubuntu, I have compiled and installed all the ogre libraries. However, when I try to compile a tutorial with this command
g++ -o otest ogre.cpp -I `pkg-config --libs --cflags OGRE OIS` -lOgreMain -lOI
and it outputs
/tmp/cc9ndjXQ.o: In function `TutorialApplication::TutorialApplication()':
ogre.cpp:(.text+0xd): undefined reference to `BaseApplication::BaseApplication()'
/tmp/cc9ndjXQ.o: In function `TutorialApplication::TutorialApplication()':
ogre.cpp:(.text+0x51): undefined reference to `BaseApplication::BaseApplication()'
/tmp/cc9ndjXQ.o: In function `TutorialApplication::~TutorialApplication()':
ogre.cpp:(.text+0xc6): undefined reference to `BaseApplication::~BaseApplication()'
/tmp/cc9ndjXQ.o: In function `TutorialApplication::~TutorialApplication()':
ogre.cpp:(.text+0x13a): undefined reference to `BaseApplication::~BaseApplication()'
/tmp/cc9ndjXQ.o: In function `TutorialApplication::~TutorialApplication()':
ogre.cpp:(.text+0x1ae): undefined reference to `BaseApplication::~BaseApplication()'
/tmp/cc9ndjXQ.o: In function `main':
ogre.cpp:(.text+0x44d): undefined reference to `BaseApplication::go()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0xc): undefined reference to `BaseApplication::frameRenderingQueued(Ogre::FrameEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x1c): undefined reference to `BaseApplication::go()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x20): undefined reference to `BaseApplication::setup()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x24): undefined reference to `BaseApplication::configure()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x28): undefined reference to `BaseApplication::chooseSceneManager()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x2c): undefined reference to `BaseApplication::createCamera()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x30): undefined reference to `BaseApplication::createFrameListener()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x38): undefined reference to `BaseApplication::destroyScene()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x3c): undefined reference to `BaseApplication::createViewports()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x40): undefined reference to `BaseApplication::setupResources()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x44): undefined reference to `BaseApplication::createResourceListener()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x48): undefined reference to `BaseApplication::loadResources()'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x4c): undefined reference to `BaseApplication::keyPressed(OIS::KeyEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x50): undefined reference to `BaseApplication::keyReleased(OIS::KeyEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x54): undefined reference to `BaseApplication::mouseMoved(OIS::MouseEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x58): undefined reference to `BaseApplication::mousePressed(OIS::MouseEvent const&, OIS::MouseButtonID)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x5c): undefined reference to `BaseApplication::mouseReleased(OIS::MouseEvent const&, OIS::MouseButtonID)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x60): undefined reference to `BaseApplication::windowResized(Ogre::RenderWindow*)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x64): undefined reference to `BaseApplication::windowClosed(Ogre::RenderWindow*)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x7c): undefined reference to `non-virtual thunk to BaseApplication::windowResized(Ogre::RenderWindow*)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x84): undefined reference to `non-virtual thunk to BaseApplication::windowClosed(Ogre::RenderWindow*)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0x9c): undefined reference to `non-virtual thunk to BaseApplication::keyPressed(OIS::KeyEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0xa0): undefined reference to `non-virtual thunk to BaseApplication::keyReleased(OIS::KeyEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0xb4): undefined reference to `non-virtual thunk to BaseApplication::mouseMoved(OIS::MouseEvent const&)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0xb8): undefined reference to `non-virtual thunk to BaseApplication::mousePressed(OIS::MouseEvent const&, OIS::MouseButtonID)'
/tmp/cc9ndjXQ.o:(.rodata._ZTV19TutorialApplication[vtable for TutorialApplication]+0xbc): undefined reference to `non-virtual thunk to BaseApplication::mouseReleased(OIS::MouseEvent const&, OIS::MouseButtonID)'
/tmp/cc9ndjXQ.o:(.rodata._ZTI19TutorialApplication[typeinfo for TutorialApplication]+0x8): undefined reference to `typeinfo for BaseApplication'
collect2: ld returned 1 exit status
Anyone know how to fix this/what I am doing wrong?
That's not a compile time error, it's a link time error.
By the looks of things, you probably aren't compiling all the *.cpp files. What file is the body of TutorialApplication's constructor in? You need to compile and link with that.
this works on ogre tutorials :
g++ -o otest *.cpp -I `pkg-config --libs --cflags OGRE OIS`

OpenGL/glut/stdc++ build errors

I'm using Ubuntu and tried using synaptic to install everything that had the word "GLUT" in it and also SDL and opengl . But still a simple program fails to compile . It shows this :
opengl1.cpp:(.text+0xe): undefined reference to `glClear'
opengl1.cpp:(.text+0x1a): undefined reference to `glBegin'
opengl1.cpp:(.text+0x2e): undefined reference to `glVertex2i'
opengl1.cpp:(.text+0x33): undefined reference to `glEnd'
opengl1.cpp:(.text+0x38): undefined reference to `glFlush'
/tmp/ccnwQeLu.o: In function `MyInit()':
opengl1.cpp:(.text+0x4c): undefined reference to `glGetString'
opengl1.cpp:(.text+0x57): undefined reference to `std::cout'
opengl1.cpp:(.text+0x5c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x6c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0x78): undefined reference to `glGetString'
opengl1.cpp:(.text+0x83): undefined reference to `std::cout'
opengl1.cpp:(.text+0x88): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x98): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0xc0): undefined reference to `glClearColor'
opengl1.cpp:(.text+0xdf): undefined reference to `glColor3f'
opengl1.cpp:(.text+0xec): undefined reference to `glPointSize'
opengl1.cpp:(.text+0xf8): undefined reference to `glMatrixMode'
opengl1.cpp:(.text+0xfd): undefined reference to `glLoadIdentity'
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccnwQeLu.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
/tmp/ccnwQeLu.o: In function `__static_initialization_and_destruction_0(int, int)':
opengl1.cpp:(.text+0x1cb): undefined reference to `std::ios_base::Init::Init()'
opengl1.cpp:(.text+0x1d0): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccnwQeLu.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
If I use g++ instead of gcc I get this:
vim opebgl1.cpp
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm
and then get this :
/tmp/ccCJBuIl.o: In function `MyInit()':
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccCJBuIl.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
So what do I really need to start working with opengl in Ubuntu?
Add "-lstdc++ -lGL" to your linker flags.
Or try to compile it like this:
g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm
(edit: added -lGLU, removed -lstdc++)
You should link it with glut and GLU as well:
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU
Files ended with .cpp must be compiled with g++. The rest of the errors are related to the linking process, and they should not happen if you build your application with the command I suggested above. If they do, make sure you have installed libglut and libglu.
Make sure you are using g++ to build your C++ files to avoid the linker errors pertaining to the C++ standard library.
To resolve the GLUT symbols you probably just need to add -lglut to your final build command.
While compiling the code you must mention some parameters according to your includes.
You already used -lGL, -lm etc.
To use gluOrtho2D the parameter to be used is -lGLU
and for other functions that begin with glut use -lglut