0

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 Answers1

0

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.

Radu C
  • 303
  • 2
  • 11
  • ||=== Build: Debug in projzozz (compiler: GNU GCC Compiler) ===| ld.exe||cannot find -lxl.lib| ||error: ld returned 1 exit status| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| – Hela Mdaini Feb 09 '23 at 13:35
  • @HelaMdaini On windows you also need to add the `lib` (or `lib64`) depending on what bitness you are building for) directory in the linker config. I think they forgot to mention that You can read the `readme.txt` file and see the steps for Visual Studio. The steps are pretty similar for Codeblocks – Radu C Feb 09 '23 at 14:05