how to integrate libxl functions in c++

294 Views Asked by At

Im trying to read excel sheets of xlsx file and convert each sheet to a csv file using libxl

i downloaded libxl 4.1.0 and followed this steps to integrate libxl in my code: https://www.libxl.com/codeblocks.html this is my code:

#include <iostream>
#include <fstream>
#include <string>
#include "libxl.h"

using namespace std;
using namespace libxl;

int main() {
  Book* book = xlCreateBook();
  if(book) {
    if(book->load("0FUP3B0YZS_results.xls")) {
      int sheetCount = book->sheetCount();
      for(int i = 0; i < sheetCount; ++i) {
        Sheet* sheet = book->getSheet(i);
        if(sheet) {
          string csvFileName = sheet->name() + ".csv";
          ofstream csvFile(csvFileName.c_str());
          if(csvFile.is_open()) {
            int rowCount = sheet->lastRow();
            for(int row = 0; row <= rowCount; ++row) {
              int colCount = sheet->lastCol();
              for(int col = 0; col <= colCount; ++col) {
                csvFile << sheet->readStr(row, col).c_str();
                if(col != colCount) {
                  csvFile << ",";
                }
              }
              csvFile << endl;
            }
            csvFile.close();
          }
        }
      }
    }
    book->release();
  }
  return 0;
}`

but i always get this eror: undefined reference to xlCreateBook Can you please tell me what to do to be recognized in my code?

1

There are 1 best solutions below

2
Radu C On

Undefined reference means the linker cannot find the implementation of that function. To be more precise, you are not linking against libxl.

Check again that you made the linker related configurations properly and that all the required files are at the configured paths.

It would also be useful that you post the full compilation/build log.