Why is my .cpp file not being processed? - c++

I'm trying to compile (make) a game source and it seems that my gRace.cpp file is being excluded or something because it keeps returning undefined reference errors for all my gRace class methods.
libtron.a(libtron_a-gGame.o): In function `gGame::StateUpdate()':
gGame.cpp:(.text+0x99e9): undefined reference to `gRace::Reset()'
libtron.a(libtron_a-gGame.o): In function `gGame::Analysis(float)':
gGame.cpp:(.text+0xad48): undefined reference to `gRace::Sync(int, int, int)'
gGame.cpp:(.text+0xad4d): undefined reference to `gRace::Done()'
gGame.cpp:(.text+0xad61): undefined reference to `gRace::Winner()'
gGame.cpp:(.text+0xb786): undefined reference to `gRace::End()'
libtron.a(libtron_a-gWinZone.o): In function `gWinZoneHack::OnEnter(gCycle*, float)':
gWinZone.cpp:(.text+0x9206): undefined reference to `gRace::ZoneHit(ePlayerNetID*)'
libtron.a(libtron_a-gWinZone.o): In function `gWinZoneHack::gWinZoneHack(eGrid*, eCoord const&, bool)':
gWinZone.cpp:(.text+0xda96): undefined reference to `gRace::NewZone(gWinZoneHack*)'
libtron.a(libtron_a-gWinZone.o): In function `gWinZoneHack::gWinZoneHack(eGrid*, eCoord const&, bool)':
gWinZone.cpp:(.text+0xdcc6): undefined reference to `gRace::NewZone(gWinZoneHack*)'
collect2: ld returned 1 exit status
I'm including the gRace.h file in both files via:
#include "gRace.h"
Any ideas on what might be causing it to not be processed?

Not including the header file would cause undefined function compiler errors. These are linker errors, which means the actual source file isn't being linked with the other files (that is, it has nothing to do with whether or not you included gRace.h in the right places). Check your build script to ensure gRace.cpp is being linked in properly

If it is an automake project you are missing gRace.cpp in your _SOURCES section in the Makefile.am.

This apprears to be a linking error and not a compilation error. Most likely, you are missing some external libraries that you should be linking against.

Related

c++ compilation error- undefined reference

My cpp file has Scilab and Octave header files. I'm trying to compile using this command
g++ -l:/usr/lib/scilab/libscilab.so -L/usr/lib/scilab -L/usr/lib/scilab -I/usr/include/octave-3.8.1 -I/usr/include/octave-3.8.1/octave -I/usr/include/scilab s_o_test_v4.cpp -o s_o_test_v4
I get the following error
s_o_test_v4.cpp:(.text+0x5f4): undefined reference to `pvApiCtx'
s_o_test_v4.cpp:(.text+0x5ff): undefined reference to `isVarMatrixType'
s_o_test_v4.cpp:(.text+0x61a): undefined reference to `pvApiCtx'
s_o_test_v4.cpp:(.text+0x644): undefined reference to `getMatrixOfDouble'
s_o_test_v4.cpp:(.text+0x743): undefined reference to `string_vector::c_str_vec() const'
s_o_test_v4.cpp:(.text+0x755): undefined reference to `octave_main'
s_o_test_v4.cpp:(.text+0x7a2): undefined reference to `octave_value::octave_value(double)'
s_o_test_v4.cpp:(.text+0x847): undefined reference to `feval(std::string const&, octave_value_list const&, int)'
s_o_test_v4.cpp:(.text+0x8a3): undefined reference to `pvApiCtx'
Both Scilab and Octave functions show error. Should I include or remove something from my compile command?
Maybe the ordering of your make command is wrong, see also
this SO question:
Please also note that libraries need to be specified on the command
line after the files needing them
That would make your command:
g++ -I/usr/include/octave-3.8.1 -I/usr/include/octave-3.8.1/octave -I/usr/include/scilab s_o_test_v4.cpp -o s_o_test_v4 -l:/usr/lib/scilab/libscilab.so -L/usr/lib/scilab -L/usr/lib/scilab

Eclipse, MinGW, Freeglut undefined reference

I've been trying to compile this OpenGL tutorial (originally purposed for Visual Studio) but I keep having 'undefined reference' errors. The compiler output is as follows:
Info: Internal Builder is used for build
g++ -o OpenGLTutorial.exe "src\\main.o" "src\\Camera.o" -lglew32 -lglu32 -lopengl32 -lfreeglut
src\main.o: In function `Z6InitGLiPPc':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:75: undefined reference to `glutInit'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:76: undefined reference to `glutSetOption'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:77: undefined reference to `glutGet'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:78: undefined reference to `glutGet'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:80: undefined reference to `glutInitDisplayMode'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:82: undefined reference to `glutInitContextVersion'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:83: undefined reference to `glutInitContextProfile'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:84: undefined reference to `glutInitContextFlags'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:86: undefined reference to `glutInitWindowPosition'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:87: undefined reference to `glutInitWindowSize'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:89: undefined reference to `glutCreateWindow'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:91: undefined reference to `glutIdleFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:92: undefined reference to `glutDisplayFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:93: undefined reference to `glutKeyboardFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:94: undefined reference to `glutKeyboardUpFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:95: undefined reference to `glutSpecialFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:96: undefined reference to `glutSpecialUpFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:97: undefined reference to `glutMouseFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:98: undefined reference to `glutMotionFunc'
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:99: undefined reference to `glutReshapeFunc'
src\main.o: In function `main':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:236: undefined reference to `glutMainLoop'
src\main.o: In function `Z9ReshapeGLii':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:249: undefined reference to `glutPostRedisplay'
src\main.o: In function `Z9DisplayGLv':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:262: undefined reference to `glutSwapBuffers'
src\main.o: In function `Z6IdleGLv':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:278: undefined reference to `glutPostRedisplay'
src\main.o: In function `Z10KeyboardGLhii':
C:\Users\jordanrich\Documents\programming\C++\GameDev\OpenGLTutorial\Debug/../src/main.cpp:313: undefined reference to `glutLeaveMainLoop'
collect2.exe: error: ld returned 1 exit status
21:39:05 Build Finished (took 1s.154ms)
Can anyone tell me what is wrong?
If you need any more information to help answer the question just ask.
Edit (Some additional info):
As you can see from the compiler output I have linked the library that these functions require and the compiler does not complain that it could not find said library. Also I placed the freeglut dll into the binary folder.
This problem was apparently mainly due to user error and was completely circumstantial. I'm posting this for the people of the future who might have this problem.
In my header file I defined FREEGLUT_STATIC. After changing this to another variable, the linker no longer produced errors. Though I can't actually check -- very easily at least -- somewhere in my freeglut library this variable was used in an ifndef statement to determine whether freeglut needed to be linked (in case it was already linked).

How can I verify linked libraries?

I am having lots of trouble getting a specific set of drivers working, called libnifalcon.
I am pretty sure that the installation was successful, but when I try to compile the example programs I get the errors:
mars#marslab:~/Documents/libnifalcon-1.0/examples/findfalcons$ g++ findfalcons.cpp
/tmp/cc8TtfGn.o: In function `runFalconTest()':
findfalcons.cpp:(.text+0x6b): undefined reference to `libnifalcon::FalconDevice::FalconDevice()'
findfalcons.cpp:(.text+0xdd): undefined reference to `libnifalcon::FalconDevice::getDeviceCount(unsigned int&)'
findfalcons.cpp:(.text+0x1bd): undefined reference to `libnifalcon::FalconDevice::open(unsigned int)'
findfalcons.cpp:(.text+0x224): undefined reference to `libnifalcon::FalconDevice::isFirmwareLoaded()'
findfalcons.cpp:(.text+0x2ac): undefined reference to `libnifalcon::FalconFirmware::loadFirmware(bool, unsigned int const&, unsigned char*)'
findfalcons.cpp:(.text+0x33b): undefined reference to `libnifalcon::FalconDevice::isFirmwareLoaded()'
findfalcons.cpp:(.text+0x3dd): undefined reference to `libnifalcon::FalconDevice::runIOLoop(unsigned int)'
findfalcons.cpp:(.text+0x504): undefined reference to `libnifalcon::FalconDevice::runIOLoop(unsigned int)'
findfalcons.cpp:(.text+0x512): undefined reference to `libnifalcon::FalconDevice::close()'
findfalcons.cpp:(.text+0x52b): undefined reference to `libnifalcon::FalconDevice::~FalconDevice()'
findfalcons.cpp:(.text+0x53f): undefined reference to `libnifalcon::FalconDevice::~FalconDevice()'
/tmp/cc8TtfGn.o: In function `void libnifalcon::FalconDevice::setFalconFirmware<libnifalcon::FalconFirmwareNovintSDK>()':
findfalcons.cpp:(.text._ZN11libnifalcon12FalconDevice17setFalconFirmwareINS_23FalconFirmwareNovintSDKEEEvv[void libnifalcon::FalconDevice::setFalconFirmware<libnifalcon::FalconFirmwareNovintSDK>()]+0x1d): undefined reference to `libnifalcon::FalconFirmwareNovintSDK::FalconFirmwareNovintSDK()'
collect2: ld returned 1 exit status
How can I verify the libraries are linked correctly? What can I do if they aren't?
You're not linking with anything, i.e.
g++ file.cpp
does not link to any libraries other than the standard library. You need to link with other modules or libraries, probably libnifalcon
g++ findfalcons.cpp -lnifalcon
or... you probably will need to do something like
g++ -L/path/to/libnifalcon findfalcons.cpp -lnifalcon
where -I tells where to look for libraries.

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.

Error when moving from OS X to linux Red-Hat

I had written my program using XCode. It works without any problems on the Mac. I was told that our projects would be compiled on Linux machines so I wanted to make sure it works on Linux before submitting. When I tried to compile on Linux, it gave me some reference errors:
/tmp/cckwoehj.o: In function `main':
main.cpp:(.text+0x9): undefined reference to `ReadFile()'
/tmp/cckwoehj.o: In function `leftSearch(NODE*, int)':
main.cpp:(.text+0x38b): undefined reference to `conflict(int**)'
main.cpp:(.text+0x3ca): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x3ec): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0x4ec): undefined reference to `conflict(int**)'
main.cpp:(.text+0x522): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0x538): undefined reference to `conflict(int**)'
/tmp/cckwoehj.o: In function `rightSearch(NODE*, int)':
main.cpp:(.text+0x992): undefined reference to `conflict(int**)'
main.cpp:(.text+0x9d4): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x9f3): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0xaf3): undefined reference to `conflict(int**)'
main.cpp:(.text+0xb29): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0xb3f): undefined reference to `conflict(int**)'
collect2: ld returned 1 exit status
The stuff I found online all talks about templates. I don't use templates. I also found one about a 32 and 64 bit OS problem which I do not have.
I have a function called ReadFile that is declared in a header. I call it from main and include that header file in main. I am not sure what the problem is. If OS X compiles and runs and Linux doesn't, I am assuming there is something OS X does internally that it thinks is obvious but Linux is not written that way. Is this a linker error? I think OS X would be programmed to do that internally.
You're declaring your functions in a header, but there's no definition, leading to undefined references when you try to turn main.cpp into an executable.
You need to compile all your source files and link them together (in the right order).
g++ -c ReadFile.cpp
#...
g++ -c main.cpp
g++ main.o ... ReadFile.o -o my_executable
Where the object files to the right rely on no unresolved symbols defined in the object files to its left on the commandline.
XCode is an IDE so I guess it handles the linking order for you.
To handle all this stuff automatically you'll have to also use an IDE and/or write a Makefile.
If you insist in a single command, yes, you can, as long as you include all source files in the line (e.g. g++ *.cpp), but this forces a full recompilation for every single change.