如何在CMake中向`add_definition`添加”L”字面前缀。

huangapple go评论67阅读模式
英文:

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:

如何在CMake中向`add_definition`添加”L”字面前缀。

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&quot;D:/test/test&quot;)

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&quot;D:/test/test&quot;)

#main.c
#include &lt;stdio.h&gt;
#include &lt;wchar.h&gt;
#include &lt;locale.h&gt;
int main() {
	setlocale(LC_ALL, &quot;&quot;);
	wchar_t *s = FIX;
	printf(&quot;%ls\n&quot;, s);
}

# output
$ cmake -S. -Bbuild &amp;&amp; cmake --build build &amp;&amp; ./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

在我告诉解决方案之前。

我感谢您的友善回答 如何在CMake中向`add_definition`添加”L”字面前缀。
@kamilCuk @Friedrich @starball

我终于找到了我的解决方案

add_definitions(-DFIX=L\&quot;\&quot;D:/test/test\&quot;\&quot;)

它清楚地显示了L"D/test/test"。

我认为&quot;&quot;是关键字表达式。我不知道为什么。

当我在预定义的文字前加上L时,引号会消失到某个地方。

有很多友善的回答告诉我解决方案。对我来说不起作用。我不知道为什么?是版本不对吗?(我的版本是3.20.3 cmake)

英文:

before I tell solution.

I appreciate with your kind answer 如何在CMake中向`add_definition`添加”L”字面前缀。
@kamilCuk @Friedrich @starball

I just finally founded my solution

add_definitions(-DFIX=L\&quot;\&quot;D:/test/test\&quot;\&quot;)

It clearly shows L"D/test/test".

I think &quot;&quot; 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)

huangapple
  • 本文由 发表于 2023年6月29日 01:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定