我正在尝试包含在我们的 C++ 项目中找到的一些 C 代码.该函数在 C 文件中是这样定义的.
I'm trying to include some C code I found in our C++ project. The function is defined like this in the C file.
#ifdef __cplusplus
extern "C" {
#endif
extern char *dtoa(double, int, int, int *, int *, char **);
extern char *g_fmt(char *, double);
extern void freedtoa(char*);
#ifdef __cplusplus
}
#endif
char *
g_fmt(register char *b, double x)
{
我包含它的 VS 项目正在创建一个 dll.该文件正在编译为 C,项目中的其他文件正在编译为 C++.
The VS project I'm including this in is creating a dll. The file is being compiled as C, other files in the project are being compiled as C++.
我添加了一个头文件以包含在我的 C++ 文件中
I added a header to include in my C++ files
#ifndef G_FMT_H
#define G_FMT_H
#ifdef __cplusplus
extern "C" {
#endif
extern char *dtoa(double, int, int, int *, int *, char **);
extern char *g_fmt(char *, double);
extern void freedtoa(char*);
#ifdef __cplusplus
}
#endif
#endif //G_FMT_H
在解决方案的另一个项目中,我包含我的标题并尝试调用 g_fmt 函数.
In another project in the solution I include my header and try calling the g_fmt function.
#include "header.h"
...
g_fmt(my_array, 2.0);
此项目正在链接到另一个项目,我可以毫无问题地调用第一个库中的 C++ 函数.但是,添加上面的行会给我一个 lnk2001 错误.
This project is linking to the other one, I'm able to call C++ functions in the first library without any issues. However, adding the above line gives me a lnk2001 error .
error LNK2001: unresolved external symbol g_fmt
我发现了一些关于混合 C 和 C++ 的其他问题,我似乎已经在正确的位置使用 extern 关键字完成了所有必要的操作,但是我仍然无法链接.在 VS2010 中我需要做什么具体的事情吗?
I've found a number of other questions about mixing C and C++ and I seem to have done everything necessary with the extern keywords in the right places, however I'm still not able to link. Is there anything specific I need to do in VS2010?
问题针对 VStudio 2010,但适用于任何版本.
The question is for VStudio 2010, but applies to any version.
在 C 或 C++ 中,考虑到一个模块可能由多个对象组成,每个对象都应该明确界定.此外,每个对象(.c 或 .cpp 文件)都应该有自己的头文件.因此,您应该有一个头文件和一个 .c(xx) 文件用于 C 函数(在您的示例中为 3).
此外,作为一般准则,在命名文件和宏时尽量保持一致:您的 header.h 文件使用 G_FMT_H 宏作为包含保护.
尝试将您的 .h 和 .c 文件重新组织为以下内容:
In C or C++, considering that a module may consist of multiple objects, every object should be clearly delimited. Also, every object (.c or .cpp file) should have its own header file. So, you should have one header file and one .c(xx) file for the C functions (3 in your example).
Also, as a general guideline, try to be consistent when naming files and macros: your header.h file uses G_FMT_H macro as include guard.
Try reorganizing your .h and .c files to something like:
functions.h:
#pragma once
#if defined(_WIN32)
# if defined(FUNCTIONS_STATIC)
# define FUNCTIONS_API
# else
# if defined(FUNCTIONS_EXPORTS)
# define FUNCTIONS_API __declspec(dllexport)
# else
# define FUNCTIONS_API __declspec(dllimport)
# endif
# endif
#else
# define FUNCTIONS_API
#endif
#if defined(__cplusplus)
extern "C" {
#endif
FUNCTIONS_API char *dtoa(double, int, int, int*, int*, char**);
FUNCTIONS_API char *g_fmt(char*, double);
FUNCTIONS_API void freedtoa(char*);
#if defined(__cplusplus)
}
#endif
和相应的实现(functions.c):
#define FUNCTIONS_EXPORTS
#include "functions.h"
char *dtoa(double, int, int, int*, int*, char**) {
//function statements
}
char *g_fmt(char*, double) {
//function statements
}
void freedtoa(char*) {
//function statements
}
头文件中需要注意的 2 件事(除了重组和重命名):
2 things to notice here (besides reorganizing and renaming) in the header file:
#include "functions.h"
)#include "functions.h"
)
类似(或相关)问题:
这篇关于从不同 VS2010 项目中的 C++ 代码调用 C 函数时出现链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!