简体   繁体   English

在C ++代码中使用K&R风格的C函数

[英]Using K&R-style C functions in C++ code

I'm trying to use oncrpc-windows project in my VC++ project in VS2010. 我正在尝试在VS2010的VC ++项目中使用oncrpc-windows项目。 The oncrpc exports functions in svc.h files without any arguments, but in fact the functions have arguments. oncrpc导出svc.h文件中没有任何参数的函数,但实际上函数有参数。 For example, the header file exports 例如,头文件导出

#define DllExport   __declspec( dllexport ) 
...
DllExport SVCXPRT *svcudp_create();

but the implementation file contains 实现文件包含

SVCXPRT * svcudp_create(sock)
    int sock;
{

    return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
}

If I try to create C file which uses exported functions, then everything is OK, the code is compiled successfully. 如果我尝试创建使用导出函数的C文件,那么一切正常,代码编译成功。 The problem is I can't use the exported function in my CPP code, the compiler stops with the following message 问题是我无法在CPP代码中使用导出的函数,编译器会停止并显示以下消息

error C2660: 'svcudp_create' : function does not take 1 arguments 错误C2660:'svcudp_create':函数不带1个参数

I tried to use extern "C" , however it didn't help. 我试图使用extern "C" ,但它没有帮助。 I'm wondering how can I use the exported functions in my VC++ project? 我想知道如何在VC ++项目中使用导出的函数? Is it really need to write own header files with correct signatures? 是否真的需要使用正确的签名编写自己的头文件?

You will need to compile any .c files which use K&R style syntax to object files before you compile your .cc or .cpp files. 在编译.cc或.cpp文件之前,您需要编译任何使用K&R样式语法的.c文件到目标文件。 This should be done in a MakeFile. 这应该在MakeFile中完成。

Example: 例:

PNAME = "ProjectName"
PROGRAM_FILES = program.cpp program.h

svc.o: svc.c svc.h
    gcc -c $< -o $@
program.o: $(PROGRAM_FILES)
    gcc -c $< -o $@
all: svc.o program.o
    gcc $< -o $(PNAME)
clean:
    -rm -f *.o $(PNAME)

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

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