【发布时间】:2017-12-21 07:34:05
【问题描述】:
我正在使用来自.cpp 文件的目标文件,我链接到.c 测试程序。
目标文件中有使用 Win32 API 的函数,例如:
std::string gettheVolumeSize()
{
//vector for the volume size
std::stringstream vs;
std::string myvolumesize;
std::vector<std::string> volumeSize;
if (!GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE))
{
}
else {
// _tprintf(TEXT("\nSystem Directory: %s"), infoBuf);
}
char* variable;
variable = getenv("SystemDrive");
std::string use = std::string(variable);
__int64 total, free;
USES_CONVERSION_EX;
std::string path;
//create path for the diskspace request
path = use + "\\\\";
printf("%s", path);
LPWSTR mypath = A2W_EX(path.c_str(), text.length());
LPCTSTR tt = reinterpret_cast<LPCTSTR>(mypath);
printf("%ls", tt);
//fill vector with volumeSize
GetDiskFreeSpaceEx(tt, NULL, (PULARGE_INTEGER)&total, (PULARGE_INTEGER)&free);
printf("%llu", total);
std::stringstream testit;
const char* totalSize;
int roundsize;
roundsize = round((((__int64)total) / (1024 * 1024 * 1024)));
vs << round((((__int64)total) / (1024 * 1024 * 1024)));
vs >> myvolumesize;
return myvolumesize;
}
我的问题是,当我在.c 测试程序中调用函数时,我现在有不同的volumeSizes。
链接选项:
cl /c /nologo /c /MT /I。测试程序.c 测试程序.c
链接 /nologo /OPT:NOREF /NXCOMPAT /DynamicBase /out:test.exe testprogramm.obj SHA1.obj it4ecidtest.obj LIBCPMT.LIB libcmt.lib libvcruntime.lib oldnames.lib kernel32.lib user32.lib netapi32.lib gdi32.lib comdlg32.lib comctl32.lib wsock32.lib shell32.lib Rpcrt4.lib oleaut32.lib Ole32.lib Wbemuuid.lib wintrust.lib crypt32.lib Ws2_32.lib iphlpapi.lib Psapi.lib advapi32.lib Shlwapi.lib dhcpcsvc.lib userenv.lib atls.lib msvcrtd.lib vcruntimed.lib netapi32.lib Advapi32.lib IPHLPAPI.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
已编辑:
当我调用目标文件中的函数时,输出为1996888586,而在普通.cpp测试程序中,总= 512003928064。
我只是使用printf 来检查值,我不会将输出返回到我的.c 文件:
#include<stdio.h>
#include "it4ecid.h"
int main() {
printf("Hello World\n");
char* cstring = getCompositeID();
printf("%s", cstring);
return 0;
}
为了清楚起见:我从我的 .cpp 文件创建一个目标文件,并在用 C 编写的测试程序中调用它的 getCompositeID() 函数。
getCompositeID 是.cpp 文件中的一个函数,现在只是调用gettheVolumeSize() 函数。
【问题讨论】:
-
不同的volumeSizes:有什么不同?请详细说明您的问题。你得到什么输出?你期望什么输出?
-
欢迎来到 stackoverflow.com。请花点时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour 和read about how to ask good questions。最后请学习如何创建Minimal, Complete, and Verifiable Example。
-
您将如何从 C 源文件调用返回 C++ 对象的 C++ 函数?也许这就是你的问题?
-
编辑:函数输出:总值
-
@objectfiles 不会描述您的代码,但会显示它。您应该考虑提供minimal reproducible example。
标签: c++ file object compilation