How to fix the Unbound module Graphics in an ocaml project

Image
From ~/pr/gitl/ocaml-gol In a constant effort to learn new programming languages, I'm currently trying to use ocaml , a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria . It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML (Meta Language) background.   The error When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result : If the project uses dune : (executable (name ocaml_project) (libraries lwt.unix graphics) ) with this code : let () = Printf.printf "Hello, world!\n";; Lwt_io.printf "Hello, world!\n";; Graphics.open_graph " 800x600";; The first times I built this project running the du

How to pass a std::string as a void* parameter

This issue comes from some research for RainbruRPG while working with openssl function (see NewProfile class). This C function is waiting for a void* parameter as passphrase, and I'm getting the user-entered password as a std::string.


 

The problem is, if you try to pass it directly, use get the following error :

error: cannot convert 'std::string' {aka 
  'std::__cxx11::basic_string'} to 'void*'

To pass it to the legacy void* function, you just need to pass a pointer to the first element of the string :

#include <string>
#include <iostream>

void
waiting_for(void* str)
{
    using std::cout;
    using std::endl;

	// You have to cast it or you'll print the pointer 
    // address instead of the string content
    cout << (char*)str << endl;
}

std::string passphrase = "whatever";
waiting_for(&passphrase[0]);

This trick also works with char* but you have to cast the result (at least on g++) to avoid an invalid conversion error :

const char* = "whatever";
waiting_for((void*)&passphrase[0]);

Comments

Popular posts from this blog

How to make a map of variant in C++