Home > Software engineering >  Issue running c code from 2013 using Clion
Issue running c code from 2013 using Clion

Time:05-08

[I have very little experience with c , and this is the first time I use it after more than 10 years].

I need to run some c code from 2013, and I am having issues doing so. I am using Clion on OSX Monterey (M1 Silicon chip). If I run a very simple script (main.cc below), I get the error

Undefined symbols for architecture arm64:
  "Hair::read(char const*, bool)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64

Is this because the code I am trying to run was written in a different version of c than the one I am using to compile it? Or is it an issue related to the architecture I am using? Thanks!

The dataset and the original code is available here.

main.cpp

#include <opencv2/opencv.hpp>
#include <iostream>
#include <Hair.h>

using namespace cv;
using namespace std;

int main() {
    const char *path = "strands00001.data";
    Hair hair;
    hair.read(path, false);
    return 0;
}

Here is the function I'm trying to call:

bool Hair::read(const char *filename, bool flip_strands /* = false */)
{
    bool ok = ends_with(filename, ".data") ?
        read_bin(filename) : read_asc(filename);

    if (!ok)
        return false;

    if (flip_strands) {
        int nstrands = strands.size();
        for (int i = 0; i < nstrands; i  )
            reverse(strands[i].begin(), strands[i].end());
    }

    // Look for a .xf file, and apply it if found
    xform xf;
    if (xf.read(xfname(filename)))
        apply_xf(xf);

    return true;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project( OpenCVTest )
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( OpenCVTest main.cpp )
target_link_libraries( OpenCVTest  ${OpenCV_LIBS})

CodePudding user response:

The problem has nothing to do with architecture or the version of C . The definition of Hair::read couldn't be found because your CMakeLists.txt wasn't compiling the file that contained it. You need to tell it to compile Hair.cpp in addition to main.cpp, like this:

add_executable( OpenCVTest main.cpp Hair.cpp )
  • Related