3D visionTroubleshooting

C++ camera SDK not declared in this scope: header and link order

The compiler's 'not declared in this scope' error means your camera SDK's C++ header never reached the file. Find it, include it, then set the link order.

LumaSpec Vision11 min read Share on LinkedIn

On this page 9 sections
  1. 01Why does the compiler say a camera SDK name has not been declared in this scope?
  2. 02Why does the SDK's C header still leave the namespace undeclared?
  3. 03How do you find the SDK's C++ header and the include path to go with it?
  4. 04Why do the follow-on errors mention variables you never declared?
  5. 05Does a correct include finish the build, or does link order still bite?
  6. 06How do you wrap the raw depth buffer in a cv::Mat without a copy?
  7. 07How do you verify the fix beyond a clean compile?
  8. 08What keeps the error from coming back on the next machine?
  9. 09Questions

You added the depth camera's SDK to a C++ capture program, the CMake configure step printed only warnings, and then the compile died: error: 'depthsdk' has not been declared on the first line that touches the SDK's namespace. That message is a missing declaration, not a broken installation and not a permissions problem. The compiler never saw the header that names the namespace. This page walks the fix in the order you meet it: find the SDK's C++ header, dodge the C-header trap, ignore the cascade of follow-on errors, set the link order so the build finishes, wrap the raw depth buffer in a cv::Mat, and pin the version so the next machine builds the same way. depthsdk here stands in for whatever namespace your SDK's samples use.

Why does the compiler say a camera SDK name has not been declared in this scope?

The compiler reached a name from your SDK's namespace before any header declared it. In practice the SDK's C++ header was never included in that file. Add the include line above the first use of the namespace and the error disappears.

'Has not been declared' is the compiler reporting an unknown name at one line in one file. Declarations come from headers; if nothing above the use declares depthsdk, the qualified name depthsdk::Context cannot resolve. A clean configure pass proves nothing here: finding packages and writing makefiles is a different stage from compiling main.cpp, and the failure only appears when the compiler reaches your file — in the log that is the Building CXX object line, not Configuring done.

// main.cpp — what the compiler sees before the fix (illustrative names)
#include <opencv2/core/core.hpp>

int main() {
    depthsdk::Context ctx;                     // error: 'depthsdk' has not been declared
    depthsdk::Device* dev = ctx.getDevice(0);  // error: 'depthsdk' has not been declared
    return 0;
}

ctx is a default-constructed depthsdk::Context and dev a pointer taken from it. Both are illustrative stand-ins, but the shape matches nearly every depth-camera SDK: a context that enumerates devices, and a device that configures and starts streams.

Why does the SDK's C header still leave the namespace undeclared?

Because a C header declares flat functions in the global namespace and no C++ classes. The namespace API you are calling lives in the SDK's separate C++ wrapper header. Include <depthsdk/depth.h> and your other lines compile, but every depthsdk:: name stays undefined.

Most depth-camera SDKs ship two interfaces from one code base. The C interface is a set of flat functions over opaque handles, built for stable ABIs and other languages; the C++ interface wraps those calls in a namespace with classes. The two header names differ by one letter, and only the C++ header declares the namespace your code calls into.

#include <depthsdk/depth.h>   // C interface: flat functions, no namespace
// #include <depthsdk/depth.hpp>  // C++ interface: namespace + classes — what the samples use

The test is one search: open the header you are including and look for the word namespace. If it is not there, that is the C interface. Swap the include to the C++ header and rebuild. Stay on one interface per program: the C++ wrapper owns the handles the C functions return, and calling both from one file is how ownership bugs start.

How do you find the SDK's C++ header and the include path to go with it?

Open a sample program that shipped with the SDK and copy its include line verbatim. Then let the build system's imported target carry the include directory. If the compiler then reports the file itself as not found, the SDK's include directory is missing from the target.

The samples are the contract: each one starts with the include line that works against the installed version, so copying beats guessing a path. Where the directory comes from depends on the build. In plain CMake the SDK's config file exports an imported target whose include directories your target inherits. In a ROS/catkin workspace the component list in package.xml and the find_package call play that role. In an IDE project it is the project's include-path setting, which the guide to linking a vendor camera SDK into a Qt CMake project covers for Qt builds.

Two neighbouring failures have different messages and different fixes:

  • fatal error: depthsdk/depth.hpp: No such file or directory — the header exists on disk but is not on the target's include path. Add the directory through the build; if the build runs in a container, the camera SDK header not found in a containerized build page covers the container cases.
  • The SDK header itself complains about OpenCV types — the SDK header includes OpenCV headers of its own, so OpenCV must be found for the target too. The camera SDK missing OpenCV headers page walks that build configuration.

Why do the follow-on errors mention variables you never declared?

They are cascades: when depthsdk::Context is unknown, the line depthsdk::Context ctx; declares nothing, so every later use of ctx — and of dev, which depended on it — reports 'not declared' too. Fix the first error in the file, rebuild, and most of the list vanishes.

The compiler reports per use, so a capture file that touches the context and device on fifteen lines produces fifteen errors from one missing include. Read strictly top-down: error number one is the root cause, the rest are echoes, and after the include goes in, the next build prints none of them. Parallel builds scramble the order across files, so fix one file at a time.

The header gets you through compilation; the linker then needs the SDK library itself, added through find_package plus target_link_libraries. With static libraries, a library must be listed before the libraries that use it or its symbols stay unresolved.

Compilation needs declarations; linking needs definitions. The moment the error text changes from 'has not been declared' to undefined reference to, you have crossed stages, and the camera SDK undefined reference guide takes over from there.

A dependency written into a build manifest does neither job by itself. Declaring the SDK in package.xml or in a catkin_package() call tells other packages what you use; it never writes your include lines and never links the library into your target. A configure-stage warning that neither depthsdk_INCLUDE_DIRS nor depthsdk_LIBRARIES is defined means that component was never found in the file: call find_package for it before declaring the dependency.

Static archives resolve left to right, so list a library before its users. This is standard single-pass linker behavior for static archives: the linker scans each archive once, in command-line order, and pulls in only the objects that satisfy symbols already undefined at that point. When several libraries define the same functions, the linker usually keeps a single definition.

What the build says Stage What it means The fix
'depthsdk' has not been declared Compile C++ header never included in this file Include the SDK's C++ header above the first use
fatal error: depthsdk/depth.hpp: No such file or directory Preprocessor Include directory not on the target Add the SDK's include directory through the build target
undefined reference to 'depthsdk::Context::getDevice()' Link Library binary never linked to this target find_package the SDK, then target_link_libraries, dependents first
Compiles on one PC, fails on another Build Two SDK versions installed Pin one version on every build machine

How do you wrap the raw depth buffer in a cv::Mat without a copy?

Construct the Mat around the SDK's frame pointer instead of copying: cv::Mat depth(cv::Size(640, 480), CV_16U, ptr, cv::Mat::AUTO_STEP) wraps a 640 × 480, 16-bit depth buffer as a zero-copy view. The constructor stores the pointer, and no pixel data moves.

With using namespace cv; in effect, the two wrappers look like this:

// Your SDK's accessor and enum names differ; its sample shows the real ones.
dev->enableStream(depthsdk::Stream::Color, 640, 480, depthsdk::Format::Bgr8, 30);
dev->enableStream(depthsdk::Stream::Depth, 640, 480, depthsdk::Format::Z16, 30);
dev->start();

Mat color(Size(640, 480), CV_8UC3,
          (void*)dev->getFrameData(depthsdk::Stream::Color), Mat::AUTO_STEP);
Mat depth(Size(640, 480), CV_16U,
          (void*)dev->getFrameData(depthsdk::Stream::Depth), Mat::AUTO_STEP);

CV_8UC3 fits 8-bit three-channel color; CV_16U fits 16-bit unsigned depth, the usual width for Z16-style streams — the 16-bit depth and 8-bit infrared guide covers what that format means downstream. AUTO_STEP tells OpenCV to assume rows are packed with no padding; if your SDK pads rows, pass the real stride in bytes as that last argument. Configure color and depth in the same block before start(): a color stream that never appears is usually an ordering problem, and the enable-order fix for a missing color stream sorts it.

index = row × width + column

To sanity-check pixels, index the frame center: row 240 and column 240 of a 640 × 480 frame give 240 × 640 + 240 = 153,840, so px[153840] on a const uint16_t* taken from depth.data is the center depth value, typically in millimeters for a 16-bit stream. A wall at 2,850 mm that drops to 400 mm when a hand enters the view is a depth path that works.

How do you verify the fix beyond a clean compile?

Rebuild to zero errors, then run a smoke test that touches the SDK for real: confirm the streams report enabled, pull 30 frames — one second at 30 fps — and check every frame pointer and Mat type before you trust the pixels.

  1. Recompile the file from scratch and confirm the log shows zero errors from the SDK's headers.
  2. Before start(), loop over the device's stream list and log each stream's enabled state; both configured streams must report enabled.
  3. Start the device and pull 30 frames — one second at 30 fps — asserting each accessor returned a non-null pointer.
  4. Wrap both Mats and assert depth.cols == 640, depth.rows == 480 and depth.type() == CV_16U (the macro's value is 2); assert color.type() == CV_8UC3.
  5. Log the center pixel, index 153,840, for all 30 frames: a value that only moves when something enters the view is a working depth path.

The failure that typically follows a clean compile happens at device open, not compilation. If the program crashes while the context is created, the context creation crash fix for 3D cameras is the next page.

What keeps the error from coming back on the next machine?

Pin the SDK version your code compiles against and install that exact version on every machine that builds the target. A different major version can rename or drop the namespace, which turns a working checkout into the same wall of 'not declared' errors on a new bench PC.

  • Print the SDK's version macros at build time and record the value with the project, so a rebuild reproduces instead of rediscovering.
  • Install exactly that version on every build machine and in CI. A second installed version is how headers and libraries drift apart; the build-time API mismatch guide shows the diagnosis.
  • Include SDK headers through imported targets, never hand-typed absolute paths, so a move between PCs changes nothing in the code.
  • Keep a team file template with the SDK's C++ include above your own headers.
  • Keep the smoke test in CI, so a version bump that drops a namespace fails in minutes at the build server, not at the bench.

The decision, stated once: 'has not been declared' means include the SDK's C++ header above the first use; 'file not found' means fix the include directory on the target; 'undefined reference' means link the library, dependents first; different machines failing differently means pin the version. Work them in that order and the capture program builds the same way on every PC you own.

If the rescue is really a rebuild — a new depth station rather than an old checkout — these are the 3D sensors in the range that pair with a C++ SDK workflow like this one:

3D sensorsHikrobot MV-DPS200P-02 Projected structured-light 3D cameraMV-DPS200P-02
Type
Stereo / structured light
$8,829.00 · quotedView and quote
3D sensorsHikrobot MV-DT01WMU ToF sensing cameraMV-DT01WMU
Type
Depth / ToF camera
Price on requestView and quote

The same include-and-link order holds when the station uses 2D area scan cameras instead; only the stream setup changes. To pick hardware, the camera finder filters by interface and resolution and the camera configurator assembles the rest of the chain, and more depth-camera fixes sit on the 3D vision topic page.

Questions

Does the order of #include lines matter for a camera SDK header?

Put the SDK's C++ header above your own headers that use its types. A well-formed C++ header is self-contained, but SDK headers in the field sometimes assume OpenCV or system headers included earlier, so the safe template order is: standard library, SDK headers, OpenCV, your own. The samples shipped with the SDK show the order its developers tested.

Can you forward-declare the SDK's classes instead of including the header?

No. A forward declaration gives you an incomplete type: enough to hold a pointer or reference, not enough to construct a Context or call a method on one. The first member call needs the full class definition, which only the header provides. Forward declarations buy compile time in large code bases; in capture code they just move the error back to the same line.

What does the phrase 'in this scope' add to the error message?

It narrows the cause to visibility at that line: the name is either never declared, declared in a different namespace, or declared only after this use. Qualifying the name as Namespace::Class fixes a namespace mismatch; moving the include above the first use fixes a declaration gap. A newer SDK version that nests the class inside an extra namespace is the third case, and qualification is its fix.

How do you find out which SDK version your code was written against?

Two places decide it: the version macros in the SDK's installed headers, and the include lines plus API names your code uses. Print the macros from a five-line program or a build-time message, and compare against the machine that last built the project cleanly. If the installed samples use names your code does not, the installed version is not the version your code was written for.

Does the same fix apply to 2D camera SDKs and not just depth cameras?

Yes. The error is C++ name lookup, not a depth feature: a GigE or USB3 area-scan SDK fails the same way when its C++ header is missing. It also links the same way through find_package and target_link_libraries. Only the stream configuration differs — one color stream instead of a color and depth pair — so the wrap-and-check section shrinks, not the fix.