在Python中,我可以写
\nfac = lambda slf, n: 1 if n == 0 else n * slf(slf, n - 1)\nprint(fac(fac, 4)) # prints 24\nRun Code Online (Sandbox Code Playgroud)\n我正在尝试在 Haskell 中复制它。然而,Haskell 不会让我这样做:
\nfac _ 0 = 1\nfac slf n = n * slf slf (n - 1)\nRun Code Online (Sandbox Code Playgroud)\n我收到此错误:
\n \xe2\x80\xa2 Occurs check: cannot construct the infinite type: t ~ t -> p -> p\n \xe2\x80\xa2 In the first argument of \xe2\x80\x98slf\xe2\x80\x99, namely \xe2\x80\x98slf\xe2\x80\x99\n In the second argument of \xe2\x80\x98(*)\xe2\x80\x99, namely \xe2\x80\x98slf slf …Run Code Online (Sandbox Code Playgroud) 这些天我在Ubuntu上用g ++共享对象(.so)文件测试-finstrument-functions.我发现了一个奇怪的行为 - 只有在静态链接库时,-finstrument-functions似乎才有效.如果我用dlopen/dlsym等链接到库,代码的功能仍然有效,但它不会调用__cyg_profile*函数.
以下是一些快速重现问题的代码:
MyLib.h
#ifndef __MYLIB_H__
#define __MYLIB_H__
class MyLib
{
public:
void sayHello();
};
#endif
Run Code Online (Sandbox Code Playgroud)
MyLib.cpp
#include "MyLib.h"
#include <iostream>
using namespace std;
void MyLib::sayHello()
{
cout<<"Hello"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
MyLibStub.cpp(.so的C接口)
#include "MyLib.h"
extern "C" void LoadMyLib ()
{
MyLib().sayHello();
}
Run Code Online (Sandbox Code Playgroud)
Trace.cpp
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
void __cyg_profile_func_enter(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
void __cyg_profile_func_exit(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
}
#endif
void __cyg_profile_func_enter(void* this_fn, void* call_site)
{
printf("entering %p\n", (int*)this_fn);
}
void __cyg_profile_func_exit(void* this_fn, void* call_site)
{ …Run Code Online (Sandbox Code Playgroud)