Home > Software design >  How to use C library in Vala
How to use C library in Vala

Time:09-29

I want to use vega library for working on dicom files. Sample code from its website is as follows:

#include <string>

#include "vega/dictionary/dictionary.h"
#include "vega/dicom/file.h"

int main() {
  // Set the dictionary file
  vega::dictionary::Dictionary::set_dictionary("/path/to/dictionary/dictionary.txt");
  
  // Read the DICOM file in
  const std::string file_name = "/path/to/dicom/file/dicom.dcm";
  vega::dicom::File file(file_name);
  
  // Print a human-friendly representation of the file to std::cout
  vega::Formatter formatter(std::cout);
  file.data_set()->log(formatter);
}

This page explains including C code, but what about C code?

This official page states that "If the library is written in C , you cannot bind it to Vala unless there is a separate C binding of the C library (e.g., LLVM). ". Hence, it seems to me that I cannot use vega library. Am I correct?

Edit: Also, will valabind / valabind-cc with swig help?

CodePudding user response:

Yes, that would be correct, I believe. You can only link a C library, without namespaces and whatnot.

To use your C library in Vala, I would say you have to either have to either a) rewrite everything in C, but this is obviously lots of work, so very undesirable, or b) find a version of your library written in plain C.

As for creating a wrapper, you would have to expose the C api from C . This question's answers can help with that. Note that this would probably include editing the source code of the library, which may be unavailable or restricted based on the license of the library.

I don't believe, as does @wohlstad, that you are unable to use a plain C library in Vala without the C api.

  • Related