-2

I'm using a DSP library called KFR to write some fairly simple command-line programs that perform various operations. Unfortunately, this library requires using the LLVM platform toolset in VS (or alternatively, not using VS) because it isn't compatible with standard MSVC.. This kinda breaks intellisense and so debugging is a bit tricky.

Anyway, I'm currently having this problem where my build is failing and returning linking errors saying I have symbols that are multiply defined and defined elsewhere. apparently I define this somewhere?

I was worried that this might have been something to do with a namespace conflict, but now I'm fairly sure it can't be, and I really have no idea where the definition conflict is. I've looked through all my dependencies and can't find anything that even looks like a redefinition of what this error is returning.

I'd really appreciate if anyone could help me track down the problem.

Code is below. Please excuse the mess, I've not been working on it for long and its far from complete (I've also only been using c++ for a couple of months).

In the precompiled header:

#pragma once

#define _USE_MATH_DEFINES

#include "targetver.h"
#include <getopt.h>
#include <stdio.h>
#include <tchar.h>
#include <all.hpp>    //kfr header
#include <math.h>
#include <complex>

main project:

#include "stdafx.h"


/*forward declarations*/
void usage(void);
int parse_args(int, char**);
double damping();
int n_factorial(int);
double ERB(double);
std::vector<std::complex<double>> expand_poly(std::vector<std::complex<double>>);
std::vector<std::complex<double>> formSOS(std::vector<std::complex<double>>, std::vector<std::complex<double>>);
std::vector<std::complex<double>> sosfun(int, int, std::vector<std::complex<double>>, std::complex<double>, int);
std::vector<std::complex<double>> zp2tf(std::vector<std::complex<double>>, std::vector<std::complex<double>>, std::vector<std::complex<double>>);


/*global variables*/
double fc = NULL;
unsigned long fs = NULL;
int order = NULL;                                                   
int type = NULL;
char* types[] = { "classic", "allpole", "onediff", "twodiff" };
const std::complex<double> i(0.0, 1.0);                                 

/* parse input arguments*/
int parse_args(int argc, char **argv)
{
int c, i;
char* p;
int tmp;

struct option longopts[] = 
    {
        { "SamplingRate",   required_argument,  NULL,   'fs' },         // makes sense to put fs first because it establishes a reasonable range for fc
        { "frequency",      required_argument,  NULL,   'fc' },
        { "Order",          required_argument,  NULL,   'n'  },
        { "type",           required_argument,  NULL,   't'  }
};

opterr = 0;
optopt = '!';

while ((c = getopt_long(argc, argv, "fs:fc:n:t", longopts, &i)) != -1)
{
    switch (c)
    {
    case 'fs':
        if (optarg == NULL)
            throw std::invalid_argument("After -fs, the filter frequency should be provided.");
        atoi(optarg) <= 0 ?
            throw std::invalid_argument("fs cannot be negative") :
            fs = atoi(optarg);
        break;

    case 'fc':
        if (optarg == NULL)
            throw std::invalid_argument("After -fc, the sampling rate should be provided.");
        (atof(optarg) <= 0 || atof(optarg) > fs / 2) ?
            throw std::invalid_argument("the filter centre frequency may not be negative or more than half the sampling rate.") :
            fc = atof(optarg);
        break;

    case 'n':
        if (optarg == NULL)
            throw std::invalid_argument("After -n, the filter order should be provided");
        atoi(optarg) <= 0 || atoi(optarg) % 2 != 0 ?
            throw std::invalid_argument("Order cannot must be a positive number that is divisible by two.") :
            order = atoi(optarg);
        break;

    case 't':
        if (optarg == NULL)
            throw std::invalid_argument("After -t, the filter type should be provided");

        for (int ii = 0; ii < 4; ++ii)
        {
            if (!strcmp(optarg, types[ii]))             // check if type optarg matches any of the given types
            {
                type = ii;
                break;
            }
        }
        if (type == NULL)                               // if still null, optarg is not a type, throw error
            throw std::invalid_argument("Unknown type argument given, see usage.");
        break;

    default:
        throw std::invalid_argument("The command line contains unknown arguments.");
    }
}
return 1;

}


int main(int argc, char** argv)
{
/* arguments - fc, fs, order, type*/

if (!parse_args(argc, argv))
    throw std::invalid_argument("Unknown arguments provided, see usage.");      // parse_args should throw an error before this, but whatever

if (order == NULL)
{
    std::cout << "No order argument (-n) was provided - the default value will be used.";
    order = 4;
}

if (type == NULL)
{
    std::cout << "No type argument (-t) was provided - the default value will be used.";
    type = 0;
}

/* Initialise type invariants */

double b = damping();                       // determines filter bandwidth
double t_theta = 2 * M_PI*fc / fs;          // theoretical pole angle

switch(type)
{
    case 0:                         // implement classic gammatone
    {
        std::complex<double> c_theta = log(exp(-b * exp(t_theta * i))) * i;                 // corrected pole angle         

        std::complex<double> pole = exp(-b - i*c_theta);
        double zero = pole.real();

        std::vector<std::complex<double>> poles = { pole, conj(pole) };

        std::complex<double> a0 = pow(abs(
            ((cos(t_theta) + i*sin(t_theta)) - exp(-b)*cos(c_theta)) /
            ((cos(t_theta) + i*sin(t_theta) - exp(-b)*cos(c_theta) + exp(-b)*i*sin(c_theta)) *
            (cos(t_theta) + i*sin(t_theta) - exp(-b) * cos(c_theta) - exp(-b) * i * sin(c_theta)))), -1);           // gain for unity at fc


    }
    case 1:                         // implement allpole gammatone
    {
        std::complex<double> c_theta = acos(2 * exp(b)*cos(t_theta) / (exp(2 * b) + 1));        // corrected pole angle

        std::vector<std::complex<double>> poles = { exp(-b - i*c_theta), conj(exp(-b - i*c_theta)) };

        std::complex<double> a0 = abs(
            1. + (2. * (-exp(-b)*cos(c_theta)))* cos(t_theta) 
               - i * (2. * (-exp(-b)*cos(c_theta))) * sin(t_theta) 
               + exp(-2 * b) * cos(2 * t_theta) 
               - i * exp(-2 * b) * sin(2 * t_theta));
    }
    case 2:                         // implement onediff gammatone
    {
        std::complex<double> c_theta = acos(2 * exp(b)*cos(t_theta) / (exp(2 * b) + 1));        // corrected pole angle for allpole stages
        std::complex<double> d_theta = acos(exp(-b) / 2 + exp(i * t_theta) / (double)2 + exp(i * t_theta) / (double)2 + exp(b) / 2 - (double)1);
                                                                                        // corrected pole angle for one-zero stages

        std::vector<std::complex<double>> a_poles = { exp(-b - i * c_theta), conj(exp(-b - i * c_theta)) };
        std::vector<std::complex<double>> d_poles = { exp(-b - i * d_theta), conj(exp(-b - i * d_theta)) };

        std::complex<double> d_zero = { 1.0, 0.0 };

        std::complex<double> a0_a = abs(
            1. + (2. * (-exp(-b)*cos(c_theta)))* cos(t_theta)
            - i * (2. * (-exp(-b)*cos(c_theta))) * sin(t_theta)
            + exp(-2 * b) * cos(2 * t_theta)
            - i * exp(-2 * b) * sin(2 * t_theta));

        std::complex<double> a0_d = abs(
            (1. + (2. * (-exp(-b) * cos(d_theta))) * cos(t_theta)
            - i * (2. * (-exp(-b) * cos(d_theta))) * sin(t_theta)
            + exp(-2 * b) * cos(2 * t_theta)
            - i* exp(-2 * b) * sin(2 * t_theta))
            / (1 + -1 * cos(t_theta) - i * (-1 * sin(t_theta))));
    }
    case 3:                         // implement twodiff gammatones
    {
        std::complex<double> c_theta = acos(2 * exp(b)*cos(t_theta) / (exp(2 * b) + 1));        // corrected pole angle for allpole stages
        std::complex<double> d_theta = acos(exp(-b) / 2 + exp(i * t_theta) / (double)2 + exp(i * t_theta) / (double)2 + exp(b) / 2 - (double)1);
                                                                                        // corrected pole angle for one-zero stages
        /*std::complex<double> a_pole = exp(-b - i * c_theta);
        std::complex<double> d_pole = exp(-b - i * d_theta);*/

        std::vector<std::complex<double>> a_poles = { exp(-b - i * c_theta), conj(exp(-b - i * c_theta))};
        std::vector<std::complex<double>> d_poles = { exp(-b - i * d_theta), conj(exp(-b - i * d_theta))};

        std::complex<double> d_zero = { 1.0, 0.0 };

        std::complex<double> a0_a = abs(
            1. + (2. * (-exp(-b)*cos(c_theta)))* cos(t_theta)
            - i * (2. * (-exp(-b)*cos(c_theta))) * sin(t_theta)
            + exp(-2 * b) * cos(2 * t_theta)
            - i * exp(-2 * b) * sin(2 * t_theta));

        std::complex<double> a0_d = abs(
            (1. + (2. * (-exp(-b) * cos(d_theta))) * cos(t_theta)
                - i * (2. * (-exp(-b) * cos(d_theta))) * sin(t_theta)
                + exp(-2 * b) * cos(2 * t_theta)
                - i* exp(-2 * b) * sin(2 * t_theta))
                / (1 + -1 * cos(t_theta) - i * (-1 * sin(t_theta))));

    }
}



return 0;
}

double damping()
{
double factor = pow(n_factorial(order - 1), 2) / (M_PI*n_factorial(2 * order - 2) * pow(2, -(2 * order - 2)));
return 2 * M_PI * factor * ERB(fc) / fs;

}

int n_factorial(int n)
{
return (n == 1 || n == 0) ? 1 : n_factorial(n - 1)*n;
}

double ERB(double f)
{
return 24.7 + f / 9.265;            
}

std::vector<std::complex<double>> expand_poly(std::vector<std::complex<double>> roots)
{
std::vector<std::complex<double>> result{1};

for (int ii = 0; ii < roots.size(); ++ii)
{
    std::vector <std::complex<double>> tmp{ result.begin(), result.end() };

    for (auto &item : tmp)
        item *= (-roots[ii]);

    result.push_back(0);
    for( int jj = 1; jj < result.size(); ++jj)
    {
        result[jj] += tmp[jj - 1];
    }
}
return result;
}


/* below is pretty much a copy of zp2sos from matlab but with all the impossible cases removed */
std::vector<std::complex<double>> formSOS(std::vector<std::complex<double>> poles, std::vector<std::complex<double>> zeroes)
{
int lz = zeroes.size();
int lp = poles.size();

std::vector<std::complex<double>> sos;

if(!lz)                     // no zeroes (all-pole)
{
    sos; // sosfun
    return sos;
}

if(!(lz % 2))               // even number of zeroes
{
    sos; // sosfun2
    sos; // sosfun
    return sos;
}

sos; // sosfun2

if (lz == lp)               // equal zeroes and poles
{
    sos; // last pole
    return sos;
}

// get num den using zp2tf
// assign sos


sos; // sosfun
return sos;

}

std::vector<std::complex<double>> sosfun(int start, int stop, std::vector<std::complex<double>> poles, std::vector<std::complex<double>> sos, int version)
{
/*switch (version)
    case 1:
    case 2:*/

std::vector<std::complex<double>> placeholder{0};
return placeholder; 
}

std::vector<std::complex<double>> zp2tf(std::vector<std::complex<double>> z, std::vector<std::complex<double>> p, std::complex<double> k)
{

std::vector<std::complex<double>> den;
std::vector<std::complex<double>> num;

std::vector<std::complex<double>> p_coeff = expand_poly(p);

for (int ii = 0; ii<p_coeff.size(); ++ii)
{
    den[ii] = p_coeff[ii].real();
}

if(!z.size())
{
    num = { {0.0, 0.0}, {0.0, 0.0}, k };
    num.insert(num.end(), den.begin(), den.end());
    // add num, den to same std::vector, return
}

std::vector<std::complex<double>> z_coeff = expand_poly(z);

for(int ii = 0; ii<z_coeff.size(); ++ii)
{
    num[ii] = (z_coeff[ii] * k).real();
}   

num.insert(num.end(), den.begin(), den.end());
return num;
}
6
  • As a rule of thumb, put custom headers after the standard ones. If your headers have an error they can cause errors in the code which comes after them, e.g. if you fail to close a scope e.g. you have "{" but no "}" then the next header after it will be inside that scope, not inside the global scope, which will cause error messages, but they'll be cryptic error messages relating to things in the standard headers.
    – Jason Lang
    Commented Mar 26, 2017 at 5:54
  • also get some indenting happening, it's impossible to read that
    – Jason Lang
    Commented Mar 26, 2017 at 5:57
  • my suggestion is to comment out everything, compile, then uncomment code one section at a time and repeat, until the error happens again.
    – Jason Lang
    Commented Mar 26, 2017 at 6:00
  • Can you reproduce this problem with less code? Seems like a lot of this code would be unnecessary to get the error to still occur. Also, please only use NULL for pointers (don't use NULL for arithmetic types, use 0 instead). Commented Mar 26, 2017 at 6:05
  • I commented everything out until there was literally nothing left but the KFR header and the build error was still showing up. Then I removed the KFR header and it builds. This has ony confused me more, because I've built projects with this header before. For this app I don't actually need ~all~ the KFR utilities, and it builds if I include the other headers independently.
    – dmb
    Commented Mar 26, 2017 at 6:36

2 Answers 2

0

Given the comments about how the kfr module causes the error even without the cpp code, perhaps kfr isn't compatible with the other headers you're using. Try this:

  • compile an empty cpp file with just the kfr header.
  • maybe it'll be missing a few things from other headers, add those in as needed, go for the minimum features needed to get it working
  • if that works, then make functions in that cpp file which call the needed functions from kfr.
  • Then, access those functions either as extern functions or from a header file.

That way you'll have a separate module (cpp file) which is only providing the resources related to kfr. You then write your own custom interface for accessing it, but none of the details of kfr are exposed to the other modules.

BTW Minimizing shared headers avoids cluttering up namespaces, and it helps to minimize compile times on large programs. Ideally, as many of your #includes as possible are only in cpp files on a "need to know" basis. If you're only using one function from a shared header in a specific cpp file, consider just declaring that function directly in the using cpp file as "extern", it'll get looked up at link time, but it won't cause a recompile if you change the header. This is also a good reason to have one class per header, and only the class method declarations in the headers, not the code itself. You then only need to #include the bare-bones stuff needed in the cpp files to understand which class is which, and changing the implementation won't cause your entire program to be recompiled.

0

Perhaps you use outdated version of KFR.

Similar issue was fixed Sep 6 2016: https://github.com/kfrlib/kfr/commit/8340aaa1bf6fd31b4f8c5c868de3025fc0e4e5c5

Try to update to the latest master version and check again.

Native Visual Studio support is planned for 2.0 release, coming next week. Then, LLVM will not be required to work with Visual Studio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.