dlopen()是一个C函数,用于在运行时动态加载共享库.因此,如果您不熟悉,该模式是:
dlopen("libpath", flag)到void *handle图书馆dlsym(handle, "object_name")得到void *object你想要的东西objectdlclose (handle)卸载库.在C++中,这是所谓的别名构造函数的完美用例std::shared_ptr.模式变为:
std::shared_ptr<void> handlefrom dlopen("libpath", flag)将在调用dlclose()析构函数时调用std::shared_ptr<void> objectfrom handle和dlsym(handle, "object_name")object任何我们想要的地方,完全忘记handle; 当object调用析构函数时,无论何时发生,dlclose()都会自动调用绚丽的图案,效果很好.但是有一个小问题.上面的图案需要从铸void*到whatever_type_object_is*.如果"object_name"引用一个函数(考虑到用例,它大部分时间都是这样),这是未定义的行为.
在C中,有一个黑客来解决这个问题.从dlopen手册页:
// ...
void *handle;
double (*cosine)(double);
// ...
handle = dlopen("libm.so", RTLD_LAZY);
// ...
/* Writing: …Run Code Online (Sandbox Code Playgroud) 我有一个球拍模块hw.rkt:
#lang racket/base
(provide hw)
(define (hw) (displayln "Hello, world!"))
Run Code Online (Sandbox Code Playgroud)
我想编写一个嵌入Racket运行时并应用该过程的C程序(hw).
有示例代码这里演示如何嵌入球拍运行时和应用过程,在racket/base,或阅读和评价的S-表达,但我没有运气修改这些代码以允许访问(hw)程序.
本页面似乎是说,这是可能做到什么,我想先编译hw.rkt使用到hw.c做raco ctool --c-mods,当我尝试这工作得很好,但我仍然无法实际访问(hw)过程.
如果有人可以发布一个完整的示例程序,或者只是描述要使用哪些C函数,我将非常感激.从那里我可以弄清楚剩下的.
编辑以提供我尝试过的事情的示例.
我修改了示例程序以摆脱"评估命令行参数"位并直接跳到REPL以便我可以进行实验.因此(用"hw.c"运行的结果raco ctool --c-mods hw.c ++libs racket/base hw.rkt):
#define MZ_PRECISE_GC
#include "scheme.h"
#include "hw.c"
static int run(Scheme_Env *e, int argc, char *argv[])
{
Scheme_Object *curout = NULL, *v = NULL, *a[2] = {NULL, NULL};
Scheme_Config *config = NULL;
int i;
mz_jmp_buf * volatile save = NULL, fresh; …Run Code Online (Sandbox Code Playgroud) 这有效:
#include <functional>
template < bool (*F)( int ) > class Foo {};
bool fooFunc( int n ) { return true; }
int main( int argc, char* argv[] )
{
auto a = Foo< fooFunc >();
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为你无法将lambda转换为函数指针:
#include <functional>
template < bool (*F)( int ) > class Foo {};
auto barFunc = [] ( int n ) -> bool { return true; };
int main( int argc, char* argv[] )
{
auto a = Foo< barFunc >();
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为你不能使用std …
搜索这个问题的标题给了我一些人引用相同的错误,但在不同的情况下,不幸的是,那里提供的答案是针对他们的情况,我不知道他们如何帮助我.
我正在尝试operator<<为模板类重载.以下是测试用例:
Vector.h:
#ifndef __INCL_VECTOR_H__
#define __INCL_VECTOR_H__
#include <array>
template < class T, unsigned int N >
class Vector
{
public:
Vector();
Vector( std::array< T, N > );
template < class U, unsigned int M > friend Vector< U, M > operator+ ( const Vector< U, M >&, const Vector< U, M >& );
template < class U, unsigned int M > friend std::ostream& operator<< ( std::ostream&, Vector< U, M >& );
T& operator[] ( const unsigned int& …Run Code Online (Sandbox Code Playgroud) 下面的 C 程序的预期行为是将其自己的可执行文件复制到一个新的随机命名的文件,然后执行该文件,令人作呕。这应该创建很多很多可执行文件的副本。这显然是一个糟糕的想法,但这仍然是我正在尝试做的事情。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char* argv[]) {
/* Obtain name of current executable */
const char* binName = argv[0];
/* Print message */
printf("Hello from %s!\n", binName);
/* Create name of new executable */
char newBinName[] = "tmpXXXXXX";
mkstemp(newBinName);
/* Determine size of current executable */
struct stat st;
stat(binName, &st);
const int binSize = st.st_size;
/* Copy current executable to memory */
char* binData = (char*) malloc(sizeof(char) * binSize); …Run Code Online (Sandbox Code Playgroud)