Inheritance Linking throws 'undefined reference to...' C++ - c++

I've writing a "big" Indexs project in C++, that is difficult for me meanwhile...
While i was trying to create an inheritance between 2 clases:
ZonalPermutant wich inherit from Permutant
i got the following error:
error:
g++ -Wall -std=c++0x lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o -o permZone
lib/Permutant.o: In function `Permutant::Permutant()':
Permutant.cpp:(.text+0x20): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(long)':
Permutant.cpp:(.text+0x8e): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(PList<long>*, long)':
Permutant.cpp:(.text+0x10c): undefined reference to `vtable for Permutant'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant()':
ZonalPermutant.cpp:(.text+0x41): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(long)':
ZonalPermutant.cpp:(.text+0xa4): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(PList<long>*, PList<long>*, long)':
ZonalPermutant.cpp:(.text+0x13e): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::~ZonalPermutant()':
ZonalPermutant.cpp:(.text._ZN14ZonalPermutantD2Ev[_ZN14ZonalPermutantD5Ev]+0x2f): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o:(.rodata._ZTI14ZonalPermutant[_ZTI14ZonalPermutant]+0x10): undefined reference to `typeinfo for Permutant'
collect2: error: ld returned 1 exit status
Makefile:17: recipe for target 'permZone' failed
make: *** [permZone] Error 1
I know it is probably that the linking in my makefile could be wrong written. So i will show you my makefile:
makefile:
CC = g++
STD = -std=c++0x
DIR = -I .
CFLAGS = -Wall -c $(STD)
LFLAGS = -Wall $(STD)
BRUTEFORCE_LIB = lib/VectorSpace.o lib/Vector.o lib/BruteForce.o lib/BruteForceMain.o
PIVOT_LIB = lib/VectorSpace.o lib/Vector.o lib/Pivot.o lib/PivotMain.o
PERM_LIB = lib/Permutants.o lib/VectorSpace.o lib/Vector.o lib/PermMain.o lib/Permutant.o
BASICS_LIB = lib/MajorOrderHeap.o lib/MinorOrderHeap.o lib/PList.o lib/OList.o lib/PList.o lib/HeapElement.o lib/Random.o lib/Tokenizer.o lib/Matrix.o
PERMZONE_LIB = lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o
default: permZone
#EXE's
#PermZone
permZone: $(PERMZONE_LIB)
$(CC) $(LFLAGS) $(PERMZONE_LIB) -o permZone
lib/PermZoneMain.o: src/PermZoneMain.cpp src/Index.h src/Space.h
$(CC) src/PermZoneMain.cpp $(CFLAGS) -o lib/PermZoneMain.o
lib/PermZone.o: src/Indexes/PermZone/PermZone.h src/Indexes/PermZone/PermZone.cpp $(BASICS_LIB)
$(CC) src/Indexes/PermZone/PermZone.cpp $(CFLAGS) -o lib/PermZone.o
lib/ZonalPermutant.o: src/Indexes/PermZone/ZonalPermutant.cpp src/Indexes/PermZone/ZonalPermutant.h lib/Permutant.o
$(CC) src/Indexes/PermZone/ZonalPermutant.cpp $(CFLAGS) -o lib/ZonalPermutant.o
lib/Permutant.o: src/Element.h src/Indexes/Permutants/Permutant.h src/Indexes/Permutants/Permutant.cpp $(BASICS_LIB)
$(CC) src/Indexes/Permutants/Permutant.cpp $(CFLAGS) -o lib/Permutant.o
and now the h and cpp files (i know its a lot of text):
Permutant.h:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../../Element.h"
#include "../../Basics/PList.h"
#ifndef PERMUTANT_H
#define PERMUTANT_H
class Permutant : public Element
{
protected:
PList<long> permutation;//stores only ID's
public:
bool isInverted;
Permutant();
~Permutant();
Permutant(long id);
Permutant(PList<long>* permutation,long id);
void setPermutation(PList<long>* permutation);
PList<long> getPermutation();
void invertPermutation();
long distance(Permutant* other);
string toString();
static long spearmanFootRule(Permutant &p1, Permutant &p2);
};
#endif // PERMUTANT_H
Permutant.cpp:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "Permutant.h"
//I dont like this trick.. but i have to use it for future distance calculations
typedef long (*P_distance)(Permutant&,Permutant&);//i hope it doesn't cause problems with inheritance :D
P_distance p_distance;
Permutant::Permutant()
{
isInverted = false;
p_distance = &spearmanFootRule;
}
Permutant::Permutant(long id)
{
this->id = id;
isInverted = false;
p_distance = &spearmanFootRule;
}
Permutant::Permutant(PList<long>* permutation,long id)
{
this->id = id;
isInverted = false;
this->permutation = *permutation;
p_distance = &spearmanFootRule;
}
void Permutant::setPermutation(PList<long>* permutation)
{
this->permutation = *permutation;
}
PList<long> Permutant::getPermutation()
{
return this->permutation;
}
void Permutant::invertPermutation()
{
PList<long> *inverted_permutation = new PList<long>(permutation.size());
inverted_permutation->toArray();
for (long i = 0; i < permutation.size(); ++i)
{
(*inverted_permutation)[permutation[i]] = i;
}
this->setPermutation(inverted_permutation);
this->isInverted = !isInverted;
}
long Permutant::distance(Permutant* other)
{
return p_distance(*this, *other);
}
string Permutant::toString()
{
ostringstream oss;
oss << ((isInverted)?"i":"")<< " " << permutation.toString();
return oss.str();
}
long Permutant::spearmanFootRule(Permutant &p1, Permutant &p2)
{
long dist = 0;
if(p1.isInverted == p2.isInverted)
{
p1.invertPermutation();
}
for (int i = 0; i < p2.getPermutation().size(); ++i)
{
dist+= abs(p1.getPermutation().get(p2.getPermutation().get(i)) - i);
}
return dist;
}
ZonalPermutant:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../Permutants/Permutant.h"
#ifndef ZONAL_PERMUTANT_H
#define ZONAL_PERMUTANT_H
class ZonalPermutant :public Permutant
{
private:
PList<long> zones;
public:
ZonalPermutant();
~ZonalPermutant(){};
ZonalPermutant(long id);
ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id);
void setZones(PList<long>* zones);
PList<long> getZones();
long distance(Permutant* other);
string toString();
};
#endif // ZONAL_PERMUTANT_H
ZonalPermutant.cpp:
//
// Created by Maximiliano Verdugo on 28/12/15.
// Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "ZonalPermutant.h"
ZonalPermutant::ZonalPermutant() : Permutant()
{}
ZonalPermutant::ZonalPermutant(long id) : Permutant(id)
{}
ZonalPermutant::ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id) : Permutant(permutation,id)
{
this->zones = *zones;
}
void ZonalPermutant::setZones(PList<long>* zones)
{
this->zones = *zones;
}
PList<long> ZonalPermutant::getZones()
{
return this->zones;
}
long ZonalPermutant::distance(Permutant* other)
{
return 0;
}
string ZonalPermutant::toString()
{
return ":D";
}
Every of the other classes used in my code are good implemented and they dont create any problem at the moment of compiling and linking others Indexes...
If there is any problem in my code or way to programm, i would like that you say that to me :).
Thanks you for the help.

Try declaring your destructors virtual (usually no harm done if not necessary), and/or add their implementations to .cpp files. In my experience
Undefined reference to `vtable for Permutant'
hints to this direction.

Related

Linking PHP-CPP library to a Linux project in Visual Studio 2017

I have a small C++ test project which should result in a .so-library on Windows Subsystem for Linux (WSL, more or less = Ubuntu 14.x). In order to get a PHP extension the current version of the PHP-CPP library is linked to the project. The compiler is g++ 4.8 on Linux and remotely operated from Visual Studio. In the properties I add -fpic for compiling and -lphpcpp to link libphpcpp.so. Using the old version of Visual Studio 2017 (until 02/2018) everything behaved well and the build was fine.
After updating to the current version of Visual Studio 2017 (15.5.6, 02/2018) the build ends in numerous linking errors of the same type: /usr/bin/ld : error : relocation x has invalid symbol index y, where the numbers x and y vary.
I have no idea what happens here.
The small project consists uses the ideas from here:
DLL in Visual Studio
... and ...
PHP-CPP first extension
The linking options recorded in Visual Studio:
-o"C:\Users\Robert\Documents\Lightning Talk\MathFuncs\MathFuncs\bin\x64\Release\MathFuncs.out" "3600000"
-Wl,-z,relro -Wl,--print-map -Wl,-z,noexecstack -Wl,--trace -Wl,
--verbose -Wl,--no-undefined "g++" -l"phpcpp" -Wl,-z,now
The last lines of the ld/g++ error messages:
1>/usr/bin/ld : error : relocation 15 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 16 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 17 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 18 has invalid symbol index 13
1>/usr/bin/ld : error : relocation 19 has invalid symbol index 21
1>/usr/bin/ld : error : relocation 0 has invalid symbol index 2
1>/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o : error :
1>collect2 : error : ld returned 1 exit status
1>Die Erstellung des Projekts "MathFuncs.vcxproj" ist abgeschlossen -- FEHLER.
My code is here:
MathFuncs.h:
#pragma once
#include <stdexcept>
using namespace std;
// This class is exported from the MathFuncsDll.dll
class MathFuncs
{
public:
// Returns a + b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
// Throws const std::invalid_argument& if b is 0
static double Divide(double a, double b);
};
double MathFuncs::Add(double a, double b)
{
return a + b;
}
double MathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
... and main.cpp:
#include <iostream>
#include <phpcpp.h>
#include "MathFuncs.h"
Php::Value phpAdd(Php::Parameters &params)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Add(a, b);
}
Php::Value phpSubtract(Php::Parameters &params)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Subtract(a, b);
}
Php::Value phpMultiply(Php::Parameters &params)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Multiply(a, b);
}
Php::Value phpDivide(Php::Parameters &params)
{
auto a(0.0);
a = params[0];
auto b(0.0);
b = params[1];
return MathFuncs::Divide(a, b);
}
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* #return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("phpMathFuncs", "1.0"); // To be humble, we can change the version number to 0.0.1
extension.add<phpAdd>("Add", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpSubtract>("Subtract", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpMultiply>("Multiply", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
extension.add<phpDivide>("Divide", {
Php::ByVal("a", Php::Type::Float),
Php::ByVal("b", Php::Type::Float)
});
// return the extension
return extension;
}
}
In the project properties/general the had been "application (.out)". Changing it to "dynamic library (.so)" solved the problem.

Command line linking for FMOD on windows, 'undefined reference' [duplicate]

This question already has answers here:
C++:Undefined reference to 'FMOD:: X'
(3 answers)
Closed 5 years ago.
I know this will probably a rather dumb question, but after two days of trying, i thought i would ask anyway. I’m quite new to C++, so I think there will be a very simple solution to this.
When I try to compile the examples in the low level examples section of the FMOD API on the command line I get always ‘undefined reference’ errors for the FMOD stuff. I know there must be a problem with the libraries, but as you can see in the makefile, I tried to include everything:
In the lib folder of the FMOD API are:
fmod_vc.lib , fmodL_vc.lib, fmod64_vc.lib, fmodL64_vc.lib, libfmod.a, libfmodL.a
I use the latest version of the FMOD API on Windows 8.1 64 bit. The problem is independent of the compiler. I tried GCC/Cygwin and CLANG. The error output is produced by MinGW. I think I searched pretty everywhere on the net. The anwsers to: C++:Undefined reference to 'FMOD:: X' are not working for me. As far as I can see, there is no more fmodex_vc.lib/fmodex64_vc.lib in the API.
Here is my makefile:
CFLAGS = -m64 -c -Wall -g --std=c++11
LFLAGS = -m64 -g -Wl,-rpath=../lib
INCLUDE_PATH = -I../inc
LIBRARY_PATH = -L../lib
LIBS = -lfmod64 -lfmodL64 -lfmod64_vc -lfmodL64_vc
all: generate_tone.o common.o common_platform.o
g++ $(LFLAGS) $(LIBRARY_PATH) generate_tone.o common.o common_platform.o -o generate.exe ../lib/libfmod.a ../lib/libfmodL.a $(LIBS)
common.o: common.cpp common.h
g++ $(CFLAGS) $(INCLUDE_PATH) common.cpp -o common.o
common_platform.o: common_platform.cpp common_platform.h
g++ $(CFLAGS) $(INCLUDE_PATH) common_platform.cpp -o common_platform.o
generate_tone.o: generate_tone.cpp
g++ $(CFLAGS) $(INCLUDE_PATH) generate_tone.cpp -o generate_tone.o
And here are the error messages:
generate_tone.o: In function `FMOD_Main()':
c:\cSandBox\fmod\examples/generate_tone.cpp:28: undefined reference to `FMOD::System::getVersion(unsigned int*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:36: undefined reference to `FMOD::System::init(int, unsigned int, void*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:42: undefined reference to `FMOD::System::createDSPByType(FMOD_DSP_TYPE, FMOD::DSP**)'
c:\cSandBox\fmod\examples/generate_tone.cpp:44: undefined reference to `FMOD::DSP::setParameterFloat(int, float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:58: undefined reference to `FMOD::ChannelControl::stop()'
c:\cSandBox\fmod\examples/generate_tone.cpp:62: undefined reference to `FMOD::System::playDSP(FMOD::DSP*, FMOD::ChannelGroup*, bool, FMOD::Channel**)'
c:\cSandBox\fmod\examples/generate_tone.cpp:64: undefined reference to `FMOD::ChannelControl::setVolume(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:66: undefined reference to `FMOD::DSP::setParameterInt(int, int)'
c:\cSandBox\fmod\examples/generate_tone.cpp:68: undefined reference to `FMOD::ChannelControl::setPaused(bool)'
c:\cSandBox\fmod\examples/generate_tone.cpp:76: undefined reference to `FMOD::ChannelControl::stop()'
c:\cSandBox\fmod\examples/generate_tone.cpp:80: undefined reference to `FMOD::System::playDSP(FMOD::DSP*, FMOD::ChannelGroup*, bool, FMOD::Channel**)'
c:\cSandBox\fmod\examples/generate_tone.cpp:82: undefined reference to `FMOD::ChannelControl::setVolume(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:84: undefined reference to `FMOD::DSP::setParameterInt(int, int)'
c:\cSandBox\fmod\examples/generate_tone.cpp:86: undefined reference to `FMOD::ChannelControl::setPaused(bool)'
c:\cSandBox\fmod\examples/generate_tone.cpp:94: undefined reference to `FMOD::ChannelControl::stop()'
c:\cSandBox\fmod\examples/generate_tone.cpp:98: undefined reference to `FMOD::System::playDSP(FMOD::DSP*, FMOD::ChannelGroup*, bool, FMOD::Channel**)'
c:\cSandBox\fmod\examples/generate_tone.cpp:100: undefined reference to `FMOD::ChannelControl::setVolume(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:102: undefined reference to `FMOD::DSP::setParameterInt(int, int)'
c:\cSandBox\fmod\examples/generate_tone.cpp:104: undefined reference to `FMOD::ChannelControl::setPaused(bool)'
c:\cSandBox\fmod\examples/generate_tone.cpp:112: undefined reference to `FMOD::ChannelControl::stop()'
c:\cSandBox\fmod\examples/generate_tone.cpp:116: undefined reference to `FMOD::System::playDSP(FMOD::DSP*, FMOD::ChannelGroup*, bool, FMOD::Channel**)'
c:\cSandBox\fmod\examples/generate_tone.cpp:118: undefined reference to `FMOD::ChannelControl::setVolume(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:120: undefined reference to `FMOD::DSP::setParameterInt(int, int)'
c:\cSandBox\fmod\examples/generate_tone.cpp:122: undefined reference to `FMOD::ChannelControl::setPaused(bool)'
c:\cSandBox\fmod\examples/generate_tone.cpp:130: undefined reference to `FMOD::ChannelControl::stop()'
c:\cSandBox\fmod\examples/generate_tone.cpp:142: undefined reference to `FMOD::ChannelControl::getVolume(float*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:149: undefined reference to `FMOD::ChannelControl::setVolume(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:157: undefined reference to `FMOD::Channel::getFrequency(float*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:162: undefined reference to `FMOD::Channel::setFrequency(float)'
c:\cSandBox\fmod\examples/generate_tone.cpp:167: undefined reference to `FMOD::System::update()'
c:\cSandBox\fmod\examples/generate_tone.cpp:176: undefined reference to `FMOD::Channel::getFrequency(float*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:178: undefined reference to `FMOD::ChannelControl::getVolume(float*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:180: undefined reference to `FMOD::ChannelControl::isPlaying(bool*)'
c:\cSandBox\fmod\examples/generate_tone.cpp:209: undefined reference to `FMOD::DSP::release()'
c:\cSandBox\fmod\examples/generate_tone.cpp:211: undefined reference to `FMOD::System::close()'
c:\cSandBox\fmod\examples/generate_tone.cpp:213: undefined reference to `FMOD::System::release()'
common_platform.o: In function `Common_Init(void**)':
c:\cSandBox\fmod\examples/common_platform.cpp:78: undefined reference to `__imp_CoInitializeEx'
common_platform.o: In function `Common_Close()':
c:\cSandBox\fmod\examples/common_platform.cpp:83: undefined reference to `__imp_CoUninitialize'
collect2.exe: error: ld returned 1 exit status
And here is the source file:
/*==============================================================================
Generate Tone Example
Copyright (c), Firelight Technologies Pty, Ltd 2004-2017.
This example shows how to play generated tones using System::playDSP
instead of manually connecting and disconnecting DSP units.
==============================================================================*/
#include "fmod.hpp"
#include "common.h"
int FMOD_Main()
{
FMOD::System *system;
FMOD::Channel *channel = 0;
FMOD::DSP *dsp;
FMOD_RESULT result;
unsigned int version;
void *extradriverdata = 0;
Common_Init(&extradriverdata);
/*
Create a System object and initialize.
*/
result = FMOD::System_Create(&system);
ERRCHECK(result);
result = system->getVersion(&version);
ERRCHECK(result);
if (version < FMOD_VERSION)
{
Common_Fatal("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);
}
result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
ERRCHECK(result);
/*
Create an oscillator DSP units for the tone.
*/
result = system->createDSPByType(FMOD_DSP_TYPE_OSCILLATOR, &dsp);
ERRCHECK(result);
result = dsp->setParameterFloat(FMOD_DSP_OSCILLATOR_RATE, 440.0f); /* Musical note 'A' */
ERRCHECK(result);
/*
Main loop
*/
do
{
Common_Update();
if (Common_BtnPress(BTN_ACTION1))
{
if (channel)
{
result = channel->stop();
ERRCHECK(result);
}
result = system->playDSP(dsp, 0, true, &channel);
ERRCHECK(result);
result = channel->setVolume(0.5f);
ERRCHECK(result);
result = dsp->setParameterInt(FMOD_DSP_OSCILLATOR_TYPE, 0);
ERRCHECK(result);
result = channel->setPaused(false);
ERRCHECK(result);
}
if (Common_BtnPress(BTN_ACTION2))
{
if (channel)
{
result = channel->stop();
ERRCHECK(result);
}
result = system->playDSP(dsp, 0, true, &channel);
ERRCHECK(result);
result = channel->setVolume(0.125f);
ERRCHECK(result);
result = dsp->setParameterInt(FMOD_DSP_OSCILLATOR_TYPE, 1);
ERRCHECK(result);
result = channel->setPaused(false);
ERRCHECK(result);
}
if (Common_BtnPress(BTN_ACTION3))
{
if (channel)
{
result = channel->stop();
ERRCHECK(result);
}
result = system->playDSP(dsp, 0, true, &channel);
ERRCHECK(result);
result = channel->setVolume(0.125f);
ERRCHECK(result);
result = dsp->setParameterInt(FMOD_DSP_OSCILLATOR_TYPE, 2);
ERRCHECK(result);
result = channel->setPaused(false);
ERRCHECK(result);
}
if (Common_BtnPress(BTN_ACTION4))
{
if (channel)
{
result = channel->stop();
ERRCHECK(result);
}
result = system->playDSP(dsp, 0, true, &channel);
ERRCHECK(result);
result = channel->setVolume(0.5f);
ERRCHECK(result);
result = dsp->setParameterInt(FMOD_DSP_OSCILLATOR_TYPE, 4);
ERRCHECK(result);
result = channel->setPaused(false);
ERRCHECK(result);
}
if (Common_BtnPress(BTN_MORE))
{
if (channel)
{
result = channel->stop();
ERRCHECK(result);
channel = 0;
}
}
if (channel)
{
if (Common_BtnDown(BTN_UP) || Common_BtnDown(BTN_DOWN))
{
float volume;
result = channel->getVolume(&volume);
ERRCHECK(result);
volume += (Common_BtnDown(BTN_UP) ? +0.1f : -0.1f);
volume = (volume > 1.0f) ? 1.0f : volume;
volume = (volume < 0.0f) ? 0.0f : volume;
result = channel->setVolume(volume);
ERRCHECK(result);
}
if (Common_BtnDown(BTN_LEFT) || Common_BtnDown(BTN_RIGHT))
{
float frequency;
result = channel->getFrequency(&frequency);
ERRCHECK(result);
frequency += (Common_BtnDown(BTN_RIGHT) ? +500.0f : -500.0f);
result = channel->setFrequency(frequency);
ERRCHECK(result);
}
}
result = system->update();
ERRCHECK(result);
{
float frequency = 0.0f, volume = 0.0f;
bool playing = false;
if (channel)
{
result = channel->getFrequency(&frequency);
ERRCHECK(result);
result = channel->getVolume(&volume);
ERRCHECK(result);
result = channel->isPlaying(&playing);
ERRCHECK(result);
}
Common_Draw("==================================================");
Common_Draw("Generate Tone Example.");
Common_Draw("Copyright (c) Firelight Technologies 2004-2017.");
Common_Draw("==================================================");
Common_Draw("");
Common_Draw("Press %s to play a sine wave", Common_BtnStr(BTN_ACTION1));
Common_Draw("Press %s to play a square wave", Common_BtnStr(BTN_ACTION2));
Common_Draw("Press %s to play a saw wave", Common_BtnStr(BTN_ACTION3));
Common_Draw("Press %s to play a triangle wave", Common_BtnStr(BTN_ACTION4));
Common_Draw("Press %s to stop the channel", Common_BtnStr(BTN_MORE));
Common_Draw("Press %s and %s to change volume", Common_BtnStr(BTN_UP), Common_BtnStr(BTN_DOWN));
Common_Draw("Press %s and %s to change frequency", Common_BtnStr(BTN_LEFT), Common_BtnStr(BTN_RIGHT));
Common_Draw("Press %s to quit", Common_BtnStr(BTN_QUIT));
Common_Draw("");
Common_Draw("Channel is %s", playing ? "playing" : "stopped");
Common_Draw("Volume %0.2f", volume);
Common_Draw("Frequency %0.2f", frequency);
}
Common_Sleep(50);
} while (!Common_BtnPress(BTN_QUIT));
/*
Shut down
*/
result = dsp->release();
ERRCHECK(result);
result = system->close();
ERRCHECK(result);
result = system->release();
ERRCHECK(result);
Common_Close();
return 0;
}
The compiling works great, did I miss a library?
In fact the solution was on C++:Undefined reference to 'FMOD:: X' but I didn't see it. I try to make it clear here for anybody, who stumbles on the same problem.
"Because the name-mangling systems for such features are not standardized across compilers, few linkers can link object code that was produced by different compilers."
https://en.wikipedia.org/wiki/Name_mangling#How_different_compilers_mangle_the_same_functions
So none of the compilers I used could cope with the Visual Studio compiled libraries.

Basic Block instrumentation

I want to insert a counter to record how many times the basic block executed.
The steps I took:
I write an LLVM instrumentation pass named BlockProfiling.cpp
I compile it with:
$ g++ -c BlockProfiling.cpp _i/usr/local/include `llvm-config --cxxflag`
it generates a file named BlockPrpfiling.o
I run:
$ g++ -shared -o BlockProfiling.so BlockProfiling.o -L/usr/local/lib `llvm-config --ldflags -libs`
it generates a shared library named BlockProfiling.so
I use it to instrument my file test.bc and generate test.bb.bc:
$opt -load=./BlockProfiling.so -insert-bb-profling test.bc -o test.bb.bc
I use llvm-diff to confirm it's done.
But when I use lli to execute test.bb.bc, there comes an error said:
LLVM ERROR: Program used external function 'llvm_start_func_profiling' which could not be resolved!
Any solutions?
The llvm version is 3.3 and here is my code:
namespace{
struct BlockProfiler: public ModulePass{
static char ID;
BlockProfiler():ModulePass(ID) {
initializeBlock ProfilerPass(*Registry::getPassRegistry());
}
virtual bool runOnModule(Module &M);
}; //end of function profiling pass
} //end of namespace
char BlockProfiler::ID = 0 ;
INITIALIZE_PASS(BlockProfiler, "insert-block-profiling",
"Insert instrumentation for profiling", false, false )
ModulePass *llvm::createBlockProfilerPass() {return new BlockProfiler() ; }
//static RegisterPass<BlockProfilerPass> Y("insert-block-profiling",
"Insert instrumentation for block profiling");
bool BlockProfiler::runOnModule(Module &M){
Function *Main = M.getFunction("main") ;
if(Main == 0)
{
errs()<<" WARING: cannot insert block profiling into a module"
<<" with no main function. \n" ;
return false ; // no main, no instrumentation, and no modify
}
unsigned NumBlocks = 0;
for(Module::iterator I = M.begin(), E = M.end(); I != E ; ++I)
NumBlocks += I->size();
Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks) ;
GlobalVariable *Counters =
new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Constant::getNullValue(ATy), "BlockProCounters") ;
//Instrument all of the blocks
unsigned i = 0;
for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I){
for(Function::iterator BB = I->begin(), E = I->end() ; BB != E; ++BB )
//Insert counter at the start of the block
IncrementCounterInBlock(BB, i++, Counters);
}
//add the initialization call to main
InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
return true;
}

NPAPI Plugin for Chrome doesn't get past NP_Initialize

I've been trying to write an extension for Chrome that uses an NPAPI plugin. I'm using mingw to compile it. I struggled originally to get Chrome to load the plugin, but now I have a different problem.
I've managed to get Chrome to call NP_GetEntryPoints and NP_Initialize, but it crashes right after that. Here's my code so far...
main.cpp :
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include <npapi.h>
#include <npfunctions.h>
#define Exported extern "C" __declspec(dllexport)
NPNetscapeFuncs NPNFuncs;
Exported NPError NP_Initialize(NPNetscapeFuncs* pFuncs) {
if (pFuncs == NULL)
return NPERR_INVALID_FUNCTABLE_ERROR;
if (HIBYTE(pFuncs->version) > NP_VERSION_MAJOR)
return NPERR_INCOMPATIBLE_VERSION_ERROR;
if (pFuncs->size < sizeof(NPNetscapeFuncs))
return NPERR_INVALID_FUNCTABLE_ERROR;
// Save functions
NPNFuncs.size = pFuncs->size;
NPNFuncs.version = pFuncs->version;
NPNFuncs.geturlnotify = pFuncs->geturlnotify;
NPNFuncs.geturl = pFuncs->geturl;
NPNFuncs.posturlnotify = pFuncs->posturlnotify;
NPNFuncs.posturl = pFuncs->posturl;
NPNFuncs.requestread = pFuncs->requestread;
NPNFuncs.newstream = pFuncs->newstream;
NPNFuncs.write = pFuncs->write;
NPNFuncs.destroystream = pFuncs->destroystream;
NPNFuncs.status = pFuncs->status;
NPNFuncs.uagent = pFuncs->uagent;
NPNFuncs.memalloc = pFuncs->memalloc;
NPNFuncs.memfree = pFuncs->memfree;
NPNFuncs.memflush = pFuncs->memflush;
NPNFuncs.reloadplugins = pFuncs->reloadplugins;
NPNFuncs.getJavaEnv = pFuncs->getJavaEnv;
NPNFuncs.getJavaPeer = pFuncs->getJavaPeer;
NPNFuncs.getvalue = pFuncs->getvalue;
NPNFuncs.setvalue = pFuncs->setvalue;
NPNFuncs.invalidaterect = pFuncs->invalidaterect;
NPNFuncs.invalidateregion = pFuncs->invalidateregion;
NPNFuncs.forceredraw = pFuncs->forceredraw;*/
// Success
MessageBoxA(0, "NP_Initialize", "Log", 0);
return NPERR_NO_ERROR;
}
Exported void NP_Shutdown() {
MessageBoxA(0, "NP_Shutdown", "Log", 0);
}
/* Entry points */
NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* savedData) {
MessageBoxA(0, "NPP_New", "Log", 0);
return NPERR_NO_ERROR;
}
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
MessageBoxA(0, "NPP_GetValue", "Log", 0);
return NPERR_NO_ERROR;
}
/*** Omitted... All the other functions are here, with just a MessageBox call in them ***/
Exported NPError NP_GetEntryPoints(NPPluginFuncs* pFuncs) {
if (pFuncs == NULL)
return NPERR_INVALID_FUNCTABLE_ERROR;
if (pFuncs->size < sizeof(NPPluginFuncs))
return NPERR_INVALID_FUNCTABLE_ERROR;
pFuncs->size = sizeof(NPPluginFuncs);
pFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
pFuncs->newp = &NPP_New;
pFuncs->destroy = &NPP_Destroy;
pFuncs->setwindow = &NPP_SetWindow;
pFuncs->newstream = &NPP_NewStream;
pFuncs->destroystream = &NPP_DestroyStream;
pFuncs->asfile = &NPP_StreamAsFile;
pFuncs->writeready = &NPP_WriteReady;
pFuncs->write = &NPP_Write;
pFuncs->print = &NPP_Print;
pFuncs->event = &NPP_HandleEvent;
pFuncs->urlnotify = &NPP_URLNotify;
pFuncs->getvalue = &NPP_GetValue;
pFuncs->setvalue = &NPP_SetValue;
pFuncs->javaClass = NULL;
pFuncs->gotfocus = &NPP_GotFocus;
pFuncs->lostfocus = &NPP_LostFocus;
pFuncs->urlredirectnotify = &NPP_URLRedirectNotify;
pFuncs->clearsitedata = &NPP_ClearSiteData;
pFuncs->getsiteswithdata = &NPP_GetSitesWithData;
MessageBoxA(0, "NP_GetEntryPoints", "Log", 0);
return NPERR_NO_ERROR;
}
I added a MessageBox call to every function, so I can see if the right functions are being called. When I run my test page, which is a blank page with an <embed> tag in it, I get a message saying NP_GetEntryPoints, then one saying NP_Initialize, then Chrome pops up a bar saying that my plugin has crashed. I think the problem is in my NP_GetEntryPoints, but I just can't see it... Is there anything I'm doing wrong, or forgetting to do?
I compile with :
g++.exe -DWIN32 -D_WIN32 -D_WINDOWS -D_WIN32_WINNT=0x0600 -D_WIN32_IE=0x0600 -D_UNICODE -DUNICODE -static-libgcc -static-libstdc++ -c "main.cpp" -o main.o
and
windres.exe "resource.rc" "resource.o"
and I link into a .DLL with :
g++.exe -Wl,--subsystem,windows -o "npplugin.dll" -s -shared main.o resource.o -lcomctl32 -lws2_32 -luxtheme -lgdi32 -lshell32 -lshlwapi
The size between function tables differs between browsers and version of browsers.
You check the size against your hard compiled library headers:
if (pFuncs->size < sizeof(NPNetscapeFuncs))
return NPERR_INVALID_FUNCTABLE_ERROR;
This can be the problem.
For example the latest (at the moment of writing gecko xulrunner SDK contains a 84 byte size struct:
typedef struct _NPPluginFuncs {
uint16_t size;
uint16_t version;
NPP_NewProcPtr newp;
NPP_DestroyProcPtr destroy;
NPP_SetWindowProcPtr setwindow;
NPP_NewStreamProcPtr newstream;
NPP_DestroyStreamProcPtr destroystream;
NPP_StreamAsFileProcPtr asfile;
NPP_WriteReadyProcPtr writeready;
NPP_WriteProcPtr write;
NPP_PrintProcPtr print;
NPP_HandleEventProcPtr event;
NPP_URLNotifyProcPtr urlnotify;
void* javaClass;
NPP_GetValueProcPtr getvalue;
NPP_SetValueProcPtr setvalue;
NPP_GotFocusPtr gotfocus;
NPP_LostFocusPtr lostfocus;
NPP_URLRedirectNotifyPtr urlredirectnotify;
NPP_ClearSiteDataPtr clearsitedata;
NPP_GetSitesWithDataPtr getsiteswithdata;
NPP_DidCompositePtr didComposite;
} NPPluginFuncs;
Chrome sends in a 80 byte struct.
So I guess the last function pointer is not in the struct used by chrome.
I would recommend using some form of logging rather than messageboxes; have you tried removing the messageboxes that are getting hit and see if you make it further? Chrome starts the plugin out of process, so it's going to expect those entrypoints to finish in a timely manner or else it'll likely kill the plugin.
Also, the --plugin-startup-dialog option may be useful to in in some cases just so you can see for sure when the plugin is starting; also, it may be useful if you wanted to attach a debugger and see if you can find more info that way. Just an FYI. I'd also try loading it in firefox; sometimes loading a plugin in another browser gives you different information. If it works in one and not the other, that can tell you something as well.
mm try to change in NP_GetEntryPoints()
pFuncs->newp = &NPP_New;
pFuncs->destroy = &NPP_Destroy;
(...)
to
pFuncs->newp = NPP_New;
pFuncs->destroy = NPP_Destroy;

Undefined reference question

When I try compiling this:
#include "OriAudioCache.hpp"
int main()
{
System *audioSystem(0);
FMOD_RESULT result;
result = System_Create(&audioSystem);
FMOD_CHECK_STATE(result);
OriAudioCache cache(audioSystem, 20);
string title("Ambitious Girl");
string path("/home/findrzkeeprz/Desktop/Resources/The_Ambitious_Girl.mp3");
cache.LoadSound(title, path, Default);
vector<OriSound>::iterator v_iter(cache.FindSound(title));
cache.PlaySound(v_iter->sound());
}
Which uses these files:
OriAudioCache.hpp
#ifndef ORI_AUDIO_CACHE_HPP_
#define ORI_AUDIO_CACHE_HPP_
#include "OriSound.hpp"
#include "OriChannel.hpp"
class OriAudioCache
{
public:
OriAudioCache(System *audioSystem, int maxChannels);
~OriAudioCache()
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter)
{
v_iter->~OriSound();
}
delete audioSystem_;
}
void LoadSound(string const& title, string const& path, AudioLoadMode mode);
vector<OriSound>::iterator FindSound(string const& title);
void RemoveSound(string const& title);
void PlaySound(Sound* sound);
vector<OriChannel>::iterator RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode);
void ReleaseChannel(Channel *channel);
private:
void inline SortChannels() {sort(channels_.begin(),channels_.end());}
vector<OriSound> audioCache_;
vector<OriChannel> channels_;
System *audioSystem_;
};
#endif
and OriAudioCache.cpp
#include "OriAudioCache.hpp"
OriAudioCache::OriAudioCache(System *audioSystem, int maxChannels)
:audioSystem_(audioSystem), channels_(maxChannels){}
void OriAudioCache::LoadSound(string const& title, string const& path, AudioLoadMode mode)
{
OriSound sound(title, path, audioSystem_, mode);
vector<OriSound>::iterator pos =lower_bound(audioCache_.begin(), audioCache_.end(), sound);
audioCache_.insert(pos, sound);
}
vector<OriSound>::iterator OriAudioCache::FindSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) return v_iter;
else continue;
}
return audioCache_.end();
}
void OriAudioCache::RemoveSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) audioCache_.erase(v_iter);
else continue;
}
}
void OriAudioCache::PlaySound(Sound* sound)
{
bool channelAlloc(false);
FMOD_CHANNELINDEX allocMode = FMOD_CHANNEL_FREE;
vector<OriChannel>::iterator oriChannel = RequestChannel(channelAlloc, allocMode);
if(channelAlloc)
{
FMOD_RESULT result;
Channel *chnl = oriChannel->channel();
result = audioSystem_->playSound(allocMode, sound, false, &chnl);
FMOD_CHECK_STATE(result);
bool isPlaying(false);
chnl->isPlaying(&isPlaying);
while(isPlaying)
{
chnl->isPlaying(&isPlaying);
}
bool paused(false);
chnl->getPaused(&paused);
if(!paused)
{
ReleaseChannel(chnl);
}
SortChannels(); //sort channels, reoder for channel requests
}
}
vector<OriChannel>::iterator OriAudioCache::RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
if(vOri_iter->status() == false)
{
if(vOri_iter->channel() == 0)
{
allocMode = FMOD_CHANNEL_FREE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else allocMode = FMOD_CHANNEL_REUSE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else return channels_.end();
}
void OriAudioCache::ReleaseChannel(Channel *channel)
{
bool playing(false);
bool paused(false);
channel->isPlaying(&playing);
channel->getPaused(&paused);
if(!playing && !paused)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
for(; vOri_iter != channels_.end(); ++vOri_iter)
{
if(vOri_iter->channel() == channel) vOri_iter->setStatus(false);
}
}
}
I get undefined reference errors:
findrzkeeprz#Aardvak:~/Documents/Chidori/Engine/Audio$ make
g++ -ggdb -I../../ -I../../Engine -I../../Include -I../../Public -o audio main.cpp ../../Libraries/FMODEX/libfmodex.so
/tmp/cctNhPVy.o: In function `main':
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:9: undefined reference to `OriAudioCache::OriAudioCache(FMOD::System*, int)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:12: undefined reference to `OriAudioCache::LoadSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, AudioLoadMode)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:13: undefined reference to `OriAudioCache::FindSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:14: undefined reference to `OriAudioCache::PlaySound(FMOD::Sound*)'
collect2: ld returned 1 exit status
make: *** [audio] Error 1
What I'm I doing wrong here?
You're compiling OriAudioCache.hpp, when you should be compiling OriAudioCache.cpp, that's assuming that file that contains the implementation.
In general, the term "Undefined reference" from a compiler (actually the linker) means that some code fragment is accessing a symbol that the linker could not find.
Common causes of these errors:
Source file containing the definition
is not included in the build process.
A source fragment (code lines) were
not removed.
A header file (class declaration)
declares the given method or symbol (which was not removed or needs to be implemented).
Incorrect or missing scope resolution
operator (namespace issue).
Build process using wrong version of
source files (e.g. file not checked
into CMS.).
Most cases of these errors are not about the C++ reference operator nor dereferencing pointers.
Solutions:
Code review by independent eye(s).
Code inspection tools: Cppcheck,
Valgrind, Klocwork, etc.
Make fewer changes, compiler more
often.
Use Test Driven Development. ;-)