英文:
how to add "L" literal prefix to add_definition in cmake
问题
I wanted to append "L" literal prefix to a C++ macro using CMake's add_definitions
.
I wanted to define a "FIX" macro like this:
#define FIX L"D:/test/test"
So I tried the following in CMake:
add_definitions("-DFIX=L\"D:/test/test\"")
I thought this was correct, but the result was very strange.
Here's the result I got:
As you can see, it correctly makes L"D:/test/test"
, but it also appended other macro options that I didn't concatenate to the end of the string (for example, \DWIN32
).
I tried many things, but I haven't found the right solution yet. Could you please give me some advice?
Thank you.
I attempted to find a solution on internet communities, but I couldn't find one. So, I tried these variations:
add_definitions("-DFIX=L\"D:/test/test\"")
add_definitions(-DFIX=L\"D:/test/test\")
add_definitions("-DFIX=L\"D:/test/test\"")
But none of them provided a solution.
英文:
I wanted to append L literal prefix to c++ macro with cmake add_definitions
I wanted to build like below "FIX" macro.
#define FIX L"D:/test/test"
so I tried
add_definitions("-DFIX=L\"\"D:/test/test\"")
I think it is right. but result was very weird.
this is my result.
enter image description here
as you can see, it correctly makes L"D:/test/test"
but it was appended other macro options what I didn't concatenated on back of string. I don't know why they appended to my macro.( for example \DWIN32)
I also tried many things. but I didn't find right solution yet.
could you give me a advice?
thank you.
I tried to find question on the internet community
but there was no solution.
so I tried
add_definitions("-DFIX=L\"\"D:/test/test\"")
add_definitions(-DFIX=L\"D:/test/test")
add_definitions("-DFIX=L\"D:/test/test\"")
but they was not a solution.
答案1
得分: 1
只需将编译定义添加如下,不需要-D
,也不需要额外的引号:
add_compile_definitions(FIX=L"D:/test/test")
add_definitions
命令已被 add_compile_definition
取代。请查看CMake文档。相对于 add_compile_definitions
,更推荐使用 target_compile_definitions
。
MCVE:
# CmakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(test)
add_executable(main main.c)
target_compile_definitions(main PUBLIC FIX=L"D:/test/test")
# main.c
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, "");
wchar_t *s = FIX;
printf("%ls\n", s);
}
输出:
$ cmake -S. -Bbuild && cmake --build build && ./build/main
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: ../build
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main
D:/test/test
英文:
Just add the compile definition as it is. No -D
. No extra quotes.
add_compile_definitions(FIX=L"D:/test/test")
Command add_definitions
has been superseded by add_compile_definition
. See CMake docs. Over add_compile_definitions
prefer target_compile_definitions
.
MCVE:
#CmakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(test)
add_executable(main main.c)
target_compile_definitions(main PUBLIC FIX=L"D:/test/test")
#main.c
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, "");
wchar_t *s = FIX;
printf("%ls\n", s);
}
# output
$ cmake -S. -Bbuild && cmake --build build && ./build/main
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: ../build
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main
D:/test/test
答案2
得分: 1
在我告诉解决方案之前。
我感谢您的友善回答
@kamilCuk @Friedrich @starball
我终于找到了我的解决方案
add_definitions(-DFIX=L\"\"D:/test/test\"\")
它清楚地显示了L"D/test/test"。
我认为""是关键字表达式。我不知道为什么。
当我在预定义的文字前加上L时,引号会消失到某个地方。
有很多友善的回答告诉我解决方案。对我来说不起作用。我不知道为什么?是版本不对吗?(我的版本是3.20.3 cmake)
英文:
before I tell solution.
I appreciate with your kind answer
@kamilCuk @Friedrich @starball
I just finally founded my solution
add_definitions(-DFIX=L\"\"D:/test/test\"\")
It clearly shows L"D/test/test".
I think "" is keyword expression. I don't know why exactly.
when I appended L to predefined literal, quotes are gone somewhere.
so many kind answer tell me a solution. it doesn't work for me. I don't know why? is it version is wrong? (my version is 3.20.3 cmake)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论