0

I am relatively new to C++, but have come from Python and C.

I am using an SDK for a lidar sensor. I have 5 main files that are involved; SDK.h, SDK.cpp, setup.h, setup.cpp and main.cpp.

A class is defined within the SDK.

rplidar.h

class RPlidarDriver{
public:
    static RPlidarDriver * CreateDriver(_u32 drivertype = DRIVER_TYPE_SERIALPORT);
    // more code
}

main.cpp

#include <iostream>
#include "rplidar.h"
#include "setup.h"

using namespace std;
using namespace rp::standalone::rplidar;

int main()
{
    //code

    RPlidarDriver* lidar = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);

    start_reading(lidar, scanMode);

    //code
}

setup.cpp

#include "setup.h"
#include "rplidar.h"

using namespace std;
using namespace rp::standalone::rplidar;

void start_reading(RPlidarDriver* driver, const char* scanMode)
{
    //start motor
    driver->startMotor();

    //more code...

}

setup.h

#include "rplidar.h"
using namespace rp::standalone::rplidar;

namespace setup
{
    void start_reading(RPlidarDriver* driver, const char* scanMode);
}

However I get this error

main.obj : error LNK2019: unresolved external symbol "void __cdecl setup::start_reading(class rp::standalone::rplidar::RPlidarDriver *,char const *)" (?start_reading@setup@@YAXPAVRPlidarDriver@rplidar@standalone@rp@@PBD@Z) referenced in function _main

I also get the same error for other functions that I try to use the object as a parameter.

If I put the function in setup.cpp into main.cpp, it compiles easily. I tried to implement & and use the parameter as a reference instead, but not luck.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
incudo
  • 1
  • 2
  • How are you compiling, and linking your files? Are you linking all of your object files together? [Good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should have explained the process of compilation. – Algirdas Preidžius Jun 27 '19 at 23:16
  • yes i am linking by defining the include directories (where the sdk files are) and compiling using visual studio – incudo Jun 27 '19 at 23:22
  • defining include directories, have nothing related with linking the files, or libraries together. Did you link with the appropriate .lib files, from the SDK, that you are using? – Algirdas Preidžius Jun 27 '19 at 23:28
  • Yes, i added the .lib file within the Linker section of the properties. I can run the class when i put the functions of setup.cpp into main.cpp. so i think that means that the SDK must be compiling correctly? But i feel like i am calling the object from within the main.cpp incorrectly. – incudo Jun 27 '19 at 23:37
  • 3
    Oh, I see the problem, for which, I am sure there's a duplicate, but am too tired to search for, at this time of day. You declared `start_reading` as belonging to `namespace setup`, but the definition is in the global scope, not the `namespace setup`. – Algirdas Preidžius Jun 28 '19 at 00:07
  • That is a good point, i will give that a try – incudo Jun 28 '19 at 00:41

1 Answers1

-2

main.cpp and setup.cpp both need to be compiled and the resulting object files need to be linked together along with the SDK libraries. The error you're getting is telling you you're trying to link the final binary using just main.o and the SDK without setup.o That's why the linker is failing to find the symbol with the implementation of your start_reading function.

  • When you say main.cpp and setup.cpp need to be compiled and linked with the SDK library, doesnt visual studio do that? The project is already linked with the .lib – incudo Jun 28 '19 at 00:35