0

I am trying to create a simple c++ project in Visual studio 2015

Peakdetector.h

 #ifndef PEAKDETECTOR_H
 #define PEAKDETECTOR_H
 //-------------------------------------------------------
 #ifdef DLL_BUILD_SETUP
    #ifdef Q_OS_LINUX
        #define DLLSPEC __attribute__((visibility("default")))
  #else
      #define DLLSPEC __declspec(dllexport)
  #endif
 #else
   #ifdef Q_OS_LINUX
      #define DLLSPEC
  #else
      #define DLLSPEC __declspec(dllimport)
   #endif
 #endif
  namespace vpg {
   #ifndef VPG_BUILD_FROM_SOURCE
   class DLLSPEC PeakDetector
  #else
   class PeakDetector
  #endif
       private:
          int __seek(int d) const;
          double __getDuration(int start, int stop);
   }

   inline int PeakDetector::__seek(int d) const
   {
     return ((m_intervalslength + (d % m_intervalslength)) % m_intervalslength);
   }

#endif

PeakDetector.cpp

#include "stdafx.h"
#include "peakdetector.h"

   namespace vpg {
     void PeakDetector::__updateInterval(double _duration)
     {
         //other stuff

     }
}

When I try to run this application i get error

LNK2019 unresolved external symbol "__declspec(dllimport) private: int __cdecl vpg::PeakDetector::__seek(int)const " (__imp_?__seek@PeakDetector@vpg@@AEBAHH@Z) referenced in function "private: void __cdecl vpg::PeakDetector::__updateInterval(double)" (?__updateInterval@PeakDetector@vpg@@AEAAXN@Z) MyCustomProject

I am new to this and cannot figure out why am I having this error.I have just copy pasted this code from an example.Please let me know if I am missing any code. Also I dont have any .lib files.

Rohit
  • 10,056
  • 7
  • 50
  • 82
  • 2
    C++ is case sensitive. –  Aug 03 '17 at 20:02
  • @NeilButterworth Thank you so much.Like I said I am very new to this. what should I change here ? is it `class peakDetector{` ?? – Rohit Aug 03 '17 at 20:02
  • @Rohit class name " class PeakDetector" – Pavan Chandaka Aug 03 '17 at 20:04
  • Right under `#else`, change `class peakDetector` to `class PeakDetector`. A fresh pair of eyes always helps. You probably also need to add an `#endif` on the line immediately after. – joshwilsonvu Aug 03 '17 at 20:04
  • 1
    Also, names like `__seek` are reserved for the C++ implementation - you should not create such names in your own code. –  Aug 03 '17 at 20:05
  • the double underscores, why would you do that :C – underscore_d Aug 03 '17 at 20:07
  • Also, your namespace is not closed in PeakDetector.h and you need `#endif`s paired with each and every `#ifdef`. I recommend commenting which `#if` each `#endif` is paired with, it will help you out later. – joshwilsonvu Aug 03 '17 at 20:08
  • @NeilButterworth I have copy-pasted this code from https://github.com/pi-null-mezon/vpglib/blob/master/Library/include/peakdetector.h – Rohit Aug 03 '17 at 20:09
  • 1
    That just means they're also wrong. Don't follow their example. More reading: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Aug 03 '17 at 20:21

1 Answers1

2

You must add the DLL_BUILD_SETUP the defines in Visual Studio.

In order to do that, yo must go to

Project Settings -> C/C++ -> Preprocessor -> Preprocessor definitions

and add the definition to the list.

You must use the spec __declspec(dllexport) when compiling the library that is exporting the symbols (in this case the class), and __declspec(dllimport) in the project that USES that library.

I see from the source code that you've provided that there is an additional definition VPG_BUILD_FROM_SOURCE which disables the export in order to using static/inline linking, you may try to add that define instead.

ichramm
  • 6,437
  • 19
  • 30
  • I did not find any typos in the post but, considering what I read in the comments: could it be that someone updated the original question and removed the typos? – ichramm Aug 03 '17 at 20:29
  • @JuanRamirez You can see the post history if you look at the edit line. [edited 16 minutes ago](https://stackoverflow.com/posts/45493544/revisions). Original post was camelCase but was updated to PascalCase. –  Aug 03 '17 at 20:33
  • Thanks @Thebluefish, my previous comment was for a user that said I was missing a typo, which in turn was fixed at that time :) – ichramm Aug 08 '17 at 15:10