简体   繁体   English

使用argv []调用类似main的函数

[英]Calling a main-like function using argv[]

I have some code here to call minizip(), a boilerplate dirty renamed main() of the minizip program, but when I compile, I get *undefined reference to `minizip(int, char**)*. 我在这里有一些代码可以调用minizip(),它是minizip程序的重命名为main()的样板文件,但是当我编译时,我得到的是对`minizip(int,char **)的未定义引用。 Here's the code. 这是代码。

int minizip(int argc, char* argv[]);

void zipFiles(void)
{
 char arg0[] = "BBG";
 char arg1[] = "-0";
 char arg2[] = "out.zip";
 char arg3[] = "server.cs";

 char* argv[] = {&arg0[0], &arg1[0], &arg2[0], &arg3[0], 0};

 int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;

 minizip(argc, argv);
}

int minizip(argc,argv)
    int argc;
    char *argv[];
{
    ...
}

Is all of that code in the same file? 所有这些代码都在同一个文件中吗? If not, and if the caller is C++ code and minizip is C code, the caller might need the minizip declaration within an extern "C" block to indicate that it will be calling a C function and therefore will need C linkage. 如果不是,并且如果调用方是C ++代码且minizip是C代码,则调用方可能需要在extern "C"块内的minizip声明来表明它将调用C函数,因此需要C链接。

(Also, don't retype error messages. Copy and paste them so that they are exact. In this case, the compiler most likely reported an undefined reference to minizip(int, char**) .) (此外,请勿重新输入错误消息。将其复制并粘贴以minizip(int, char**) 。在这种情况下,编译器很可能报告了对minizip(int, char**)的未定义引用。)

Why are you declaring the function arguments again in: 为什么在以下位置再次声明函数参数:

int minizip(argc,argv)
    int argc;
    char *argv[];
{
    ...
}

It' should say 应该说

int minizip(int argc,char *argv[])
    {
        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM