2013-03-30 86 views
2

我正在尝试将C++函数(作为字符串)发送到tcc编译器(libtcc.dll)的方式来集成Visual C++ 2012和TCC。我已经添加了libtcc.h头文件,但我不确定如何添加libtcc.dll,因为没有相应的.lib文件。我正在使用TCC发行版中的libtcc_test.c文件作为我的Win32 main()函数。TCC与Visual Studio 2012

这里是我的主:

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include "stdafx.h" 
#include "libtcc.h" 

int add(int a, int b) 
{ 
    return a + b; 
} 

char my_program[] = 
"int fib(int n)\n" 
"{\n" 
" if (n <= 2)\n" 
"  return 1;\n" 
" else\n" 
"  return fib(n-1) + fib(n-2);\n" 
"}\n" 
"\n" 
"int foo(int n)\n" 
"{\n" 
" printf(\"Hello World!\\n\");\n" 
" printf(\"fib(%d) = %d\\n\", n, fib(n));\n" 
" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n" 
" return 0;\n" 
"}\n"; 

int main(int argc, char **argv) 
{ 
    TCCState *s; 
    int (*func)(int); 

    s = tcc_new(); 
    if (!s) { 
     fprintf(stderr, "Could not create tcc state\n"); 
     exit(1); 
    } 

    /* if tcclib.h and libtcc1.a are not installed, where can we find them */ 
    if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) 
     tcc_set_lib_path(s, argv[1]+9); 

    /* MUST BE CALLED before any compilation */ 
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY); 

    if (tcc_compile_string(s, my_program) == -1) 
     return 1; 

    /* as a test, we add a symbol that the compiled program can use. 
     You may also open a dll with tcc_add_dll() and use symbols from that */ 
    tcc_add_symbol(s, "add", add); 

    /* relocate the code */ 
    if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) 
     return 1; 

    /* get entry symbol */ 
    func = tcc_get_symbol(s, "foo"); 
    if (!func) 
     return 1; 

    /* run the code */ 
    func(32); 

    /* delete the state */ 
    tcc_delete(s); 

    return 0; 
} 

当我尝试运行它,我得到在Visual Studio 2012以下错误:

是否有人有一个解决的办法?

谢谢!

+0

tcc分布很少更新。邮件列表上的大多数人会建议您从存储库中获取最新的代码并从那里进行。见http://repo.or.cz/w/tinycc.git – mikijov

+0

我下载的是http://download.savannah.gnu.org/releases/tinycc/,它只有一个月(0.9.26) 。 –

回答

0

我认为最简单的方法是制作一个.lib文件,然后用它进行链接。请参阅http://msdn.microsoft.com/en-us/library/0b9xe492.aspx上的MSDN页面,了解如何从.def文件创建.lib文件。在此之后,您可以使用指定.lib的常规方式进行链接。

+0

好吧,我用你的链接从libtcc.def建立lib文件,谢谢你。我得到的示例代码下降到一个错误:“不能从'void *'转换为'int(__cdecl *)(int)”....我解决了这个搜索结果,并在这里找到了同样的问题:[link] (http://www.cplusplus.com/forum/general/17725/)。现在我运行它,它说它找不到libtcc.dll。我如何解决这个问题?对我来说,这只是一个简单的Visual Studio链接问题。我想静态绑定到我的应用程序的DLL。 –

+0

DLL未找到问题应该像在可执行文件所在的同一目录中复制DLL一样简单。在进行调试时,请确保将工作目录设置为与可执行文件相同。这是必要的,因为默认情况下VS会使您的项目文件夹成为工作目录。 – mikijov

+0

当前的TCC分发不提供libtcc作为静态库。主要原因是因为如果您链接到一个静态库,您将不得不手动解决所有库依赖关系(您必须链接所有库libtcc需求),如果它们提供了dll,这些全部解决。 – mikijov