我正在使用一个包含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编译项目并避免未解决的符号错误?
答案 0 :(得分:2)
您需要使用extern "C"
或包含相关的headerfile [preferred]以确保它具有相同的名称。该名称因C ++函数而受到损坏,但extern "C"
的名称不会受损,因此包含标题的调用将生成&#34; unmangled&#34; name,函数定义将具有错位名称 - &gt;链接器无法找到它。
答案 1 :(得分:2)
有什么问题?
dtoa_grisu3
中的extern C
具有 C链接
C文件中的dtoa_grisu3
具有 C ++链接
它们是不同的符号,这意味着C文件中的定义不适用于头文件中的声明。
因此,在链接后,您将获得&#34; 未解析的外部符号&#34;错误。
<强>解决方案:强>
将它们全部放入或放出extern C