VC #define EC(x) L##x 作用说明

本文详细解析了C语言中宏定义的使用技巧,包括连接符##、字符串化操作符#及#@的应用,并通过实例展示了这些操作符如何在宏定义中工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define EC(x) L##x 作用说明

表示L与x连接。
#define Conn(x,y) x##y
#define ToChar(x) #@x
#define ToString(x) #x

x##y表示什么?表示x连接y,举例说:
int  n = Conn(123,456);  结果就是n=123456;
char* str = Conn("asdf", "adf")结果就是 str = "asdfadf";
怎么样,很神奇吧

再来看#@x,其实就是给x加上单引号,结果返回是一个const char。举例说:
char a = ToChar(1);结果就是a='1';
做个越界试验char a = ToChar(123);结果是a='3';
但是如果你的参数超过四个字符,编译器就给给你报错了!error C2015: too many characters in constant   :P

最后看看#x,估计你也明白了,他是给x加双引号
char* str = ToString(123132);就成了str="123132";

## 连接符与# 符

##连接符号由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成一个新的子串。但它不可以是第一个或者最后一个子串。所谓的子串 (token)就是指编译器能够识别的最小语法单元。具体的定义在编译原理里有详尽的解释,但不知道也无所谓。同时值得注意的是#符是把传递过来的参数当成字符串进行替代。下面来看看它们是怎样工作的。这是MSDN上的一个例子。

假设程序中已经定义了这样一个带参数的宏:
#define paster( n ) printf( "token" #n " = %d", token##n )

同时又定义了一个整形变量:
int token9 = 9;

现在在主程序中以下面的方式调用这个宏:
paster( 9 );

那么在编译时,上面的这句话被扩展为:
printf( "token" "9" " = %d", token9 );

注意到在这个例子中,paster(9);中的这个”9”被原封不动的当成了一个字符串,与”token”连接在了一起,从而成为了token9。而#n也被”9”所替代。

可想而知,上面程序运行的结果就是在屏幕上打印出token9=9
---------------------------------------------------------------

#define display(name) printf(""#name"")
int main() {
display(name);
}
====================================
特殊性就在于它是个宏,宏里面处理#号就如LS所说!
处理后就是一个附加的字符串!

但printf(""#name"") ;就不行了!
---------------------------------------------------------------

#define display(name) printf(""#name"")

该定义 字符串化name,
得到结果其实就是 printf("name")
(前后的空字符串拿掉)

这样输出来的自然是 name

从另外一个角度讲,
#是一个连接符号,
参与运算了, 自然不会输出了 ...

另外还有:
#define A(x) T_##x
#define B(x) #@x
#define C(x) #x
我们假设:x=1,则有:
A(1)------)T_1
B(1)------)'1'
C(1)------)"1"

转自:http://blog.21ic.com/user1/1772/archives/2007/44501.html

#是C语言中宏定义里的一个操作符
英文名叫Stringizing Operator
中文名我也不知道
简单说,它的意思就是使用参数被双引号包住
#@是使参数用单引号包住
##是粘结
看MSDN上鸟语写的也太费劲
我就给你举个例子
就比如你这样定义之后
#define print(x) printf(#x)
在程序中写
print(123);
实际上是相当于
printf("123");
就直接输出123
如果
print("123");
就相当于
printf("\"123\"");
就输出"123"
再补充一下
如果
print("\n");
就相当于
printf("\"\\n");
就输出
"\n"


printf的#

C语言中#号和##号的使用

#define display(name) printf(""#name"")
int main() {
display(name);
}
运行结果是name,为什么不是"#name"呢?
---------------------------------------------------------------

#在这里是字符串化的意思
printf(""#name"") 相当于
printf("" "name" "")
---------------------------------------------------------------

The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants
---------------------------------------------------------------

printf("" #name "") <1>
相当于printf("" "name" "") <2>
而<2>中的第2,3个“中间时空格 等价于("空+name+空')
---------------------------------------------------------------

Traceback (most recent call last): File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\utils\cpp_extension.py", line 2107, in _run_ninja_build subprocess.run( File "D:\anaconda\envs\pytorch2.3.1\lib\subprocess.py", line 528, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\TrainCNO.py", line 160, in <module> output_pred_batch = model(input_batch) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\CNOModule.py", line 476, in forward x = self.lift(x) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\CNOModule.py", line 151, in forward x = self.inter_CNOBlock(x) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\CNOModule.py", line 104, in forward return self.activation(x) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\training\filtered_networks.py", line 392, in forward x = filtered_lrelu.filtered_lrelu(x=x, fu=self.up_filter, fd=self.down_filter, b=self.bias.to(x.dtype), File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\torch_utils\ops\filtered_lrelu.py", line 114, in filtered_lrelu if impl == 'cuda' and x.device.type == 'cuda' and _init(): File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\torch_utils\ops\filtered_lrelu.py", line 26, in _init _plugin = custom_ops.get_plugin( File "D:\pycharm\pythonProject\ConvolutionalNeuralOperator-main\torch_utils\custom_ops.py", line 136, in get_plugin torch.utils.cpp_extension.load(name=module_name, build_directory=cached_build_dir, File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\utils\cpp_extension.py", line 1309, in load return _jit_compile( File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\utils\cpp_extension.py", line 1719, in _jit_compile _write_ninja_file_and_build_library( File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\utils\cpp_extension.py", line 1832, in _write_ninja_file_and_build_library _run_ninja_build( File "D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\utils\cpp_extension.py", line 2123, in _run_ninja_build raise RuntimeError(message) from e RuntimeError: Error building extension 'filtered_lrelu_plugin': [1/4] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\nvcc --generate-dependencies-with-compile --dependency-output filtered_lrelu_ns.cuda.o.d -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcompiler /EHsc -Xcompiler /wd4068 -Xcompiler /wd4067 -Xcompiler /wd4624 -Xcompiler /wd4190 -Xcompiler /wd4018 -Xcompiler /wd4275 -Xcompiler /wd4267 -Xcompiler /wd4244 -Xcompiler /wd4251 -Xcompiler /wd4819 -Xcompiler /MD -DTORCH_EXTENSION_NAME=filtered_lrelu_plugin -DTORCH_API_INCLUDE_EXTENSION_H -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\torch\csrc\api\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\TH -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include" -ID:\anaconda\envs\pytorch2.3.1\Include -D_GLIBCXX_USE_CXX11_ABI=0 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_89,code=compute_89 -gencode=arch=compute_89,code=sm_89 -std=c++17 --use_fast_math --allow-unsupported-compiler -c C:\Users\Administrator\AppData\Local\torch_extensions\torch_extensions\Cache\py39_cu118\filtered_lrelu_plugin\2e9606d7cf844ec44b9f500eaacd35c0-nvidia-geforce-rtx-4060-ti\filtered_lrelu_ns.cu -o filtered_lrelu_ns.cuda.o [2/4] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\nvcc --generate-dependencies-with-compile --dependency-output filtered_lrelu_rd.cuda.o.d -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcompiler /EHsc -Xcompiler /wd4068 -Xcompiler /wd4067 -Xcompiler /wd4624 -Xcompiler /wd4190 -Xcompiler /wd4018 -Xcompiler /wd4275 -Xcompiler /wd4267 -Xcompiler /wd4244 -Xcompiler /wd4251 -Xcompiler /wd4819 -Xcompiler /MD -DTORCH_EXTENSION_NAME=filtered_lrelu_plugin -DTORCH_API_INCLUDE_EXTENSION_H -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\torch\csrc\api\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\TH -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include" -ID:\anaconda\envs\pytorch2.3.1\Include -D_GLIBCXX_USE_CXX11_ABI=0 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_89,code=compute_89 -gencode=arch=compute_89,code=sm_89 -std=c++17 --use_fast_math --allow-unsupported-compiler -c C:\Users\Administrator\AppData\Local\torch_extensions\torch_extensions\Cache\py39_cu118\filtered_lrelu_plugin\2e9606d7cf844ec44b9f500eaacd35c0-nvidia-geforce-rtx-4060-ti\filtered_lrelu_rd.cu -o filtered_lrelu_rd.cuda.o [3/4] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\nvcc --generate-dependencies-with-compile --dependency-output filtered_lrelu_wr.cuda.o.d -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcompiler /EHsc -Xcompiler /wd4068 -Xcompiler /wd4067 -Xcompiler /wd4624 -Xcompiler /wd4190 -Xcompiler /wd4018 -Xcompiler /wd4275 -Xcompiler /wd4267 -Xcompiler /wd4244 -Xcompiler /wd4251 -Xcompiler /wd4819 -Xcompiler /MD -DTORCH_EXTENSION_NAME=filtered_lrelu_plugin -DTORCH_API_INCLUDE_EXTENSION_H -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\torch\csrc\api\include -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\TH -ID:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include" -ID:\anaconda\envs\pytorch2.3.1\Include -D_GLIBCXX_USE_CXX11_ABI=0 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_89,code=compute_89 -gencode=arch=compute_89,code=sm_89 -std=c++17 --use_fast_math --allow-unsupported-compiler -c C:\Users\Administrator\AppData\Local\torch_extensions\torch_extensions\Cache\py39_cu118\filtered_lrelu_plugin\2e9606d7cf844ec44b9f500eaacd35c0-nvidia-geforce-rtx-4060-ti\filtered_lrelu_wr.cu -o filtered_lrelu_wr.cuda.o [4/4] "D:\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64/link.exe" filtered_lrelu.o filtered_lrelu_wr.cuda.o filtered_lrelu_rd.cuda.o filtered_lrelu_ns.cuda.o /nologo /DLL c10.lib c10_cuda.lib torch_cpu.lib torch_cuda.lib -INCLUDE:?warp_size@cuda@at@@YAHXZ torch.lib /LIBPATH:D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\lib torch_python.lib /LIBPATH:D:\anaconda\envs\pytorch2.3.1\libs "/LIBPATH:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64" cudart.lib /out:filtered_lrelu_plugin.pyd FAILED: filtered_lrelu_plugin.pyd "D:\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64/link.exe" filtered_lrelu.o filtered_lrelu_wr.cuda.o filtered_lrelu_rd.cuda.o filtered_lrelu_ns.cuda.o /nologo /DLL c10.lib c10_cuda.lib torch_cpu.lib torch_cuda.lib -INCLUDE:?warp_size@cuda@at@@YAHXZ torch.lib /LIBPATH:D:\anaconda\envs\pytorch2.3.1\lib\site-packages\torch\lib torch_python.lib /LIBPATH:D:\anaconda\envs\pytorch2.3.1\libs "/LIBPATH:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\lib\x64" cudart.lib /out:filtered_lrelu_plugin.pyd 正在创建库 filtered_lrelu_plugin.lib 和对象 filtered_lrelu_plugin.exp filtered_lrelu.o : error LNK2019: 无法解析的外部符号 __std_find_trivial_1,函数 "char const * __cdecl std::_Find_vectorized<char const ,char>(char const * const,char const * const,char)" (??$_Find_vectorized@$$CBDD@std@@YAPEBDQEBD0D@Z) 中引用了该符号 ninja: build stopped: subcommand failed.这是怎么回事
最新发布
07-06
<think>好的,我现在需要帮助用户解决他们在运行PyTorch代码时遇到的错误。用户提供的错误信息看起来比较复杂,但主要是关于编译自定义CUDA扩展时的问题。让我仔细分析一下错误日志。 首先,错误发生在执行`ninja`构建时,具体是链接阶段。错误信息显示有一个无法解析的外部符号`__std_find_trivial_1`,这个符号是在一个名为`_Find_vectorized`的函数中被引用的。这通常意味着链接器找不到对应的库或者某些依赖项没有正确配置。 接下来,我需要考虑可能的原因。用户使用了PyTorch的自定义CUDA扩展,这可能涉及到多个步骤的构建过程。常见的问题包括CUDA工具包版本不兼容、Visual Studio的编译器版本不匹配、或者PyTorch版本与CUDA版本不一致。 首先,检查CUDA和PyTorch的兼容性。用户使用的是CUDA 11.8,而PyTorch的版本是2.3.1。根据PyTorch官网的安装指南,PyTorch 2.3.1应该支持CUDA 11.8,所以这一步可能没问题,但需要确认用户是否正确安装了对应的版本。 然后,查看编译器部分。用户使用的是Visual Studio 2019的MSVC 14.29.30133。PyTorch扩展通常需要与CUDA兼容的编译器版本。例如,CUDA 11.x通常需要Visual Studio 2019的某个特定版本,但需要确认MSVC的版本是否与CUDA 11.8兼容。可能存在的问题是,编译器版本过旧或过新,导致某些符号无法解析。 另外,错误中的`__std_find_trivial_1`可能与C++标准库的实现有关。这可能是因为在编译时使用了不同的C++标准或不同的运行时库,导致链接时符号不匹配。例如,是否在项目中混合使用了不同版本的MSVC运行时库,或者是否启用了某些特定的编译选项导致冲突。 还可能存在第三方库或自定义代码的问题。用户提到的`filtered_lrelu`是一个自定义的CUDA操作,可能其代码中存在与当前环境不兼容的地方。例如,某些函数在特定的编译器版本下无法正确编译,或者缺少必要的依赖库。 可能的解决方案包括: 1. 更新或降级PyTorch版本以确保与CUDA和编译器兼容。 2. 检查Visual Studio的版本是否符合CUDA工具包的要求,必要时更新Visual Studio。 3. 确保所有相关的环境变量和路径设置正确,尤其是CUDA和编译器的路径。 4. 清理并重新构建扩展,可能之前的构建缓存有问题。 5. 修改编译选项,例如调整C++标准或运行时库的设置。 6. 检查自定义CUDA代码是否有兼容性问题,可能需要更新或修改代码。 此外,用户可能需要查看具体的构建日志,寻找更详细的错误信息,或者尝试在更干净的环境中重新安装依赖项。如果问题依旧,可能需要参考PyTorch官方文档或相关社区的支持讨论。 </think>您遇到的错误是由于在编译自定义CUDA扩展(`filtered_lrelu`)时,链接器无法解析标准库符号`__std_find_trivial_1`导致的。以下是关键原因和解决方案: --- ### **错误原因** 1. **编译器版本不兼容** - 使用的 **MSVC 14.29.30133**(Visual Studio 2019)可能与当前环境(如CUDA 11.8或PyTorch 2.3.1)不完全兼容。 - `__std_find_trivial_1`是MSVC标准库的内部符号,未正确解析可能表明编译器与CUDA工具链或PyTorch的预编译库存在版本冲突。 2. **CUDA与PyTorch版本不匹配** - PyTorch 2.3.1 需要 CUDA 11.8,但需确保: - CUDA驱动版本(通过`nvidia-smi`检查)≥ 12.1。 - cuDNN版本与CUDA 11.8兼容。 3. **自定义CUDA扩展编译失败** - `filtered_lrelu`依赖的代码可能未适配当前编译器或CUDA版本,导致链接阶段失败。 --- ### **解决方案** 1. **升级Visual Studio和MSVC编译器** - 升级到 **Visual Studio 2022**(MSVC 14.3x+),CUDA 11.8官方推荐使用VS2022。 - 安装[MSVC v143工具链](https://visualstudio.microsoft.com/vs/community/)(随VS2022提供)。 2. **验证CUDA和PyTorch兼容性** - 运行以下命令检查CUDA驱动版本: ```bash nvidia-smi ``` - 确保PyTorch版本与CUDA匹配: ```bash python -c "import torch; print(torch.__version__, torch.version.cuda)" ``` 应输出类似`2.3.1 11.8`。 3. **清理并重新构建扩展** - 删除PyTorch扩展缓存目录: ```bash # Windows路径示例 rm -rf C:\Users\Administrator\AppData\Local\torch_extensions\ ``` - 重新运行训练脚本,强制重建扩展。 4. **使用预编译的`filtered_lrelu`版本** - 如果`filtered_lrelu`来自第三方库(如StyleGAN3),尝试安装预编译包: ```bash pip install ninja pip install torch==2.3.1+cu118 torchvision==0.14.1+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 ``` 5. **修改编译选项(临时绕过)** - 在`filtered_lrelu`的CUDA代码中,禁用向量化查找(可能引发符号冲突的功能): ```cpp // 在filtered_lrelu.cu中添加宏定义 #define _DISABLE_VECTORIZED_FIND_ ``` --- ### **示例修复步骤** ```bash # 1. 清理缓存 rm -rf C:\Users\Administrator\AppData\Local\torch_extensions\ # 2. 升级PyTorch到匹配CUDA 11.8的版本 pip install torch==2.3.1+cu118 torchvision==0.14.1+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 # 3. 安装Ninja构建工具 pip install ninja # 4. 重新运行训练脚本 python TrainCNO.py ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值