0

I have tried for a while now to use the Magick++ libraries in my project but even though I have linked to the include files and lib files in my CMake files, I still get a linker error whenever using the library in my code.

Here is my header file where I have tried to make my class and use the Image class from Magick++. Beneath that is my CMakeLists.txt file where I have linked the lib files and the set the include paths. The last picture is the error I get, where it says that there are undefined references to the Image class when I try to instantiate the class in my main.cpp file after I have used the MinGW Makefiles generator with CMake. And lastly my main.cpp file where I try to make an object from the class CnnNet from CnnNet.h

#pragma once

#include <boost/filesystem.hpp>
#include <Eigen/Eigen>

#include "Magick++.h"

#include <vector>
#include <fstream>
#include <iostream>

using namespace std;
using namespace Magick;

class CnnNet{
 public:
  CnnNet(string folderPathX){
    remove("trainFile.txt");

    fstream myFile;
    myFile.open("trainFiles.txt");

    int lengPathX = 0;
    for(auto & entry:boost::filesystem::directory_iterator(folderPathX))
      myFile << entry.path() << "\n";lengPathX++;

    string line;
    ifstream file("trainFile.txt");

    while(getline(file,line)){
      char middle[line.length()];
      string fileMiddle = "";
      strcpy(middle,line.c_str());
      char seper = '\\';

      for(int i = 0;i<line.length();i++){
    if(middle[i] == seper){
      for(int inner = i+1;i<line.length()-2;inner++){
        fileMiddle = fileMiddle+middle[inner];
        cout << fileMiddle << endl;
      }
      break;
    }
      }
      Image picture;
      picture.read("train/"+fileMiddle);
    }
  };
};

My CMakeLists.txt file:

cmake_minimum_required(VERSION 3.20.2)
project(blackjackAI)
add_executable(blackjackAI main.cpp)

set(rootPath "C:/Users/Ejer/Desktop/c++/c++Dep")

add_definitions(-DMAGICKCORE_QUANTUM_DEPTH=16)
add_definitions(-DMAGICKCORE_HDRI_ENABLE=0)

target_include_directories(blackjackAI PRIVATE ${rootPath}/boost_1_77_0)
target_include_directories(blackjackAI PRIVATE ${rootPath}/eigen-3.4.0)
target_include_directories(blackjackAI PRIVATE ${rootPath}/ImageMagick-6.8.8/include/ImageMagick-6)
target_include_directories(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/blackjackAI)

target_link_libraries(blackjackAI PRIVATE ${rootPath}/boost_1_77_0/stage/lib/libboost_filesystem-gcc10-mt-x64-1_77.a)
target_link_libraries(blackjackAI PRIVATE ${rootPath}/ImageMagick-6.8.8/lib/libMagick++-6.Q16.dll.a)
target_link_libraries(blackjackAI PRIVATE ${rootPath}/ImageMagick-6.8.8/lib/libMagickCore-6.Q16.dll.a)
target_link_libraries(blackjackAI PRIVATE ${rootPath}/ImageMagick-6.8.8/lib/libMagickWand-6.Q16.dll.a)

My error:

$ mingw32-make
Consolidate compiler generated dependencies of target blackjackAI
[ 50%] Building CXX object CMakeFiles/blackjackAI.dir/main.cpp.obj
[100%] Linking CXX executable blackjackAI.exe
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x3f7): undefined reference to `Magick::Image::Image()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x42d): undefined reference to `Magick::Image::read(std::str
ing const&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x44b): undefined reference to `Magick::Image::~Image()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x51a): undefined reference to `Magick::Image::~Image()'
collect2: error: ld returned 1 exit status
CMakeFiles\blackjackAI.dir\build.make:102: recipe for target 'blackjackAI.exe' failed
mingw32-make[2]: *** [blackjackAI.exe] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/blackjackAI.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/blackjackAI.dir/all] Error 2
Makefile:89: recipe for target 'al

My main.cpp file:

#include <vector>
#include <fstream>
#include <iostream>

#include "CnnNet.h"

using namespace Magick;
using namespace std;

int main(){

  CnnNet obj("C:/Users/Ejer/Desktop/c++/blackjackAI/train");
  
  return 0;
}
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ulrich Eckhardt Sep 06 '21 at 20:01
  • I have seen that question but I still dont see where I make a mistake during linking process – Samuel Cadell Sep 06 '21 at 20:05
  • 1
    For a start, I would reduce this as far as possible ([mcve]) and try to find out how to link this using the commandline. After all, the issues could be caused by CMake. – Ulrich Eckhardt Sep 06 '21 at 20:23
  • @UlrichEckhardt Yeah but I have a lot of other things that depend on CMake so I cant just switch to the command line – Samuel Cadell Sep 06 '21 at 20:33

0 Answers0