gpt4 book ai didi

c++ - c/c++ 集成和编译 - 未解析的外部符号,即使已定义

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:45 24 4
gpt4 key购买 nike

我正在处理一个 C++ 项目,它也包含一个 c 文件(c 文件编译选项是/TC),项目编译一切顺利。

然后我需要使用一些 Pinvokes 因此我需要使用/clr 编译项目但是我必须使用/TP 作为 C 文件并且当我这样做时大喊:

未解析的外部符号 dtoa_grisu3但是该函数是在 .c 文件下定义的,它只会在我使用/TP 编译时大喊大叫。

这是标题:

#pragma once

extern "C"
{

/// Converts the given double-precision floating point number to a string representation.
/** For most inputs, this string representation is the
shortest such, which deserialized again, returns the same bit
representation of the double.
@param v The number to convert.
@param dst [out] The double-precision floating point number will be written here
as a null-terminated string. The conversion algorithm will write at most 25 bytes
to this buffer. (null terminator is included in this count).
The dst pointer may not be null.
@return the number of characters written to dst, excluding the null terminator (which
is always written) is returned here. */

int dtoa_grisu3(double v, char *dst);

}

#ifdef __cplusplus

#include <string>
std::string dtoa_grisu3_string(double v);

#endif

这是植入(在c文件中):

int dtoa_grisu3(double v, char *dst)
{
int d_exp, len, success, decimals, i;
uint64_t u64 = CAST_U64(v);
char *s2 = dst;
assert(dst);

// Prehandle NaNs
if ((u64 << 1) > 0xFFE0000000000000ULL) return sprintf(dst, "NaN(%08X%08X)", (uint32_t)(u64 >> 32), (uint32_t)u64);
// Prehandle negative values.
if ((u64 & D64_SIGN) != 0) { *s2++ = '-'; v = -v; u64 ^= D64_SIGN; }
// Prehandle zero.
if (!u64) { *s2++ = '0'; *s2 = '\0'; return (int)(s2 - dst); }
// Prehandle infinity.
if (u64 == D64_EXP_MASK) { *s2++ = 'i'; *s2++ = 'n'; *s2++ = 'f'; *s2 = '\0'; return (int)(s2 - dst); }

success = grisu3(v, s2, &len, &d_exp);
// If grisu3 was not able to convert the number to a string, then use old sprintf (suboptimal).
if (!success) return sprintf(s2, "%.17g", v) + (int)(s2 - dst);

// We now have an integer string of form "151324135" and a base-10 exponent for that number.
// Next, decide the best presentation for that string by whether to use a decimal point, or the scientific exponent notation 'e'.
// We don't pick the absolute shortest representation, but pick a balance between readability and shortness, e.g.
// 1.545056189557677e-308 could be represented in a shorter form
// 1545056189557677e-323 but that would be somewhat unreadable.
decimals = MIN(-d_exp, MAX(1, len-1));
if (d_exp < 0 && len > 1) // Add decimal point?
{
for(i = 0; i < decimals; ++i) s2[len-i] = s2[len-i-1];
s2[len++ - decimals] = '.';
d_exp += decimals;
// Need scientific notation as well?
if (d_exp != 0) { s2[len++] = 'e'; len += i_to_str(d_exp, s2+len); }
}
else if (d_exp < 0 && d_exp >= -3) // Add decimal point for numbers of form 0.000x where it's shorter?
{
for(i = 0; i < len; ++i) s2[len-d_exp-1-i] = s2[len-i-1];
s2[0] = '.';
for(i = 1; i < -d_exp; ++i) s2[i] = '0';
len += -d_exp;
}
// Add scientific notation?
else if (d_exp < 0 || d_exp > 2) { s2[len++] = 'e'; len += i_to_str(d_exp, s2+len); }
// Add zeroes instead of scientific notation?
else if (d_exp > 0) { while(d_exp-- > 0) s2[len++] = '0'; }
s2[len] = '\0'; // grisu3 doesn't null terminate, so ensure termination.
return (int)(s2+len-dst);
}

如何使用/clr 编译项目并避免未解析的符号错误?

最佳答案

您需要使用 extern "C" 或包含相关的头文件 [首选] 以确保它获得相同的名称。 C++ 函数的名称会被损坏,但 extern "C" 不会损坏名称,因此包含 header 的调用将生成“未损坏”的名称,其中函数定义将具有损坏的名称-> 链接器找不到它。

关于c++ - c/c++ 集成和编译 - 未解析的外部符号,即使已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com