CMake: Get root project name
Suppose I have following C project structure:
├── CMakeLists.txt
├── include
│ ├── header.h
├── README.md
├── src
│ └── src.c
└── test
├── CMakeLists.txt
└── test.c
The CMakeLists.txt
in the root directory contains a project called cproj
PROJECT(cproj C)
Also in test/CMakeLists.txt
, I have another project
PROJECT(test C)
The question is how can I assess the project name cproj
in the CMakeLists.txt
in test
directory?
c cmake project
add a comment |
Suppose I have following C project structure:
├── CMakeLists.txt
├── include
│ ├── header.h
├── README.md
├── src
│ └── src.c
└── test
├── CMakeLists.txt
└── test.c
The CMakeLists.txt
in the root directory contains a project called cproj
PROJECT(cproj C)
Also in test/CMakeLists.txt
, I have another project
PROJECT(test C)
The question is how can I assess the project name cproj
in the CMakeLists.txt
in test
directory?
c cmake project
1
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52
add a comment |
Suppose I have following C project structure:
├── CMakeLists.txt
├── include
│ ├── header.h
├── README.md
├── src
│ └── src.c
└── test
├── CMakeLists.txt
└── test.c
The CMakeLists.txt
in the root directory contains a project called cproj
PROJECT(cproj C)
Also in test/CMakeLists.txt
, I have another project
PROJECT(test C)
The question is how can I assess the project name cproj
in the CMakeLists.txt
in test
directory?
c cmake project
Suppose I have following C project structure:
├── CMakeLists.txt
├── include
│ ├── header.h
├── README.md
├── src
│ └── src.c
└── test
├── CMakeLists.txt
└── test.c
The CMakeLists.txt
in the root directory contains a project called cproj
PROJECT(cproj C)
Also in test/CMakeLists.txt
, I have another project
PROJECT(test C)
The question is how can I assess the project name cproj
in the CMakeLists.txt
in test
directory?
c cmake project
c cmake project
edited Jan 1 at 9:47
tjysdsg
asked Jan 1 at 8:19
tjysdsgtjysdsg
5017
5017
1
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52
add a comment |
1
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52
1
1
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52
add a comment |
2 Answers
2
active
oldest
votes
If I gather your use case correctly, you should probably do as follows:
test
is quite likely not a standalone sub-project. It should not have aproject
directive. If you remove it, it will now be under thecproj
project.Now you looking for the project name you've set in the root, it should be available via the
CMAKE_PROJECT_NAME
variable.
According to the document, it saysthe name of the current project.
. But I have another project intest/CMakeLists.txt
. So this won't work, will it?
– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project intest
? Is it not the test directory ofcproj
? It's not standalone, is it?
– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
add a comment |
I found the solution and I guess I'll answer it myself.
In the root CMakeLists.txt
, I added SET_PROPERTY(GLOBAL PROPERTY GLOBAL_NAME "cproj")
. This created a global property named GLOBAL_NAME
.
And in the CMakeLists.txt
in test
directory, I use GET_PROPERTY(ROOT_PROJECT_NAME GLOBAL PROPERTY GLOBAL_NAME)
to store the value of the property in a local variable called ROOT_PROJECT_NAME
. Then I can access the value via that variable.
EDIT: According to @StoryTeller, I can just delete the declaration of new project in test/CMakeLists.txt
and use CMAKE_PROJECT_NAME
instead.
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994016%2fcmake-get-root-project-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I gather your use case correctly, you should probably do as follows:
test
is quite likely not a standalone sub-project. It should not have aproject
directive. If you remove it, it will now be under thecproj
project.Now you looking for the project name you've set in the root, it should be available via the
CMAKE_PROJECT_NAME
variable.
According to the document, it saysthe name of the current project.
. But I have another project intest/CMakeLists.txt
. So this won't work, will it?
– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project intest
? Is it not the test directory ofcproj
? It's not standalone, is it?
– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
add a comment |
If I gather your use case correctly, you should probably do as follows:
test
is quite likely not a standalone sub-project. It should not have aproject
directive. If you remove it, it will now be under thecproj
project.Now you looking for the project name you've set in the root, it should be available via the
CMAKE_PROJECT_NAME
variable.
According to the document, it saysthe name of the current project.
. But I have another project intest/CMakeLists.txt
. So this won't work, will it?
– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project intest
? Is it not the test directory ofcproj
? It's not standalone, is it?
– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
add a comment |
If I gather your use case correctly, you should probably do as follows:
test
is quite likely not a standalone sub-project. It should not have aproject
directive. If you remove it, it will now be under thecproj
project.Now you looking for the project name you've set in the root, it should be available via the
CMAKE_PROJECT_NAME
variable.
If I gather your use case correctly, you should probably do as follows:
test
is quite likely not a standalone sub-project. It should not have aproject
directive. If you remove it, it will now be under thecproj
project.Now you looking for the project name you've set in the root, it should be available via the
CMAKE_PROJECT_NAME
variable.
edited Jan 1 at 9:51
answered Jan 1 at 9:43
StoryTellerStoryTeller
101k12204273
101k12204273
According to the document, it saysthe name of the current project.
. But I have another project intest/CMakeLists.txt
. So this won't work, will it?
– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project intest
? Is it not the test directory ofcproj
? It's not standalone, is it?
– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
add a comment |
According to the document, it saysthe name of the current project.
. But I have another project intest/CMakeLists.txt
. So this won't work, will it?
– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project intest
? Is it not the test directory ofcproj
? It's not standalone, is it?
– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
According to the document, it says
the name of the current project.
. But I have another project in test/CMakeLists.txt
. So this won't work, will it?– tjysdsg
Jan 1 at 9:46
According to the document, it says
the name of the current project.
. But I have another project in test/CMakeLists.txt
. So this won't work, will it?– tjysdsg
Jan 1 at 9:46
@tjysdsg - Why would you define another project in
test
? Is it not the test directory of cproj
? It's not standalone, is it?– StoryTeller
Jan 1 at 9:46
@tjysdsg - Why would you define another project in
test
? Is it not the test directory of cproj
? It's not standalone, is it?– StoryTeller
Jan 1 at 9:46
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
I made test standalone, I just realized that's a stupid move
– tjysdsg
Jan 1 at 9:48
add a comment |
I found the solution and I guess I'll answer it myself.
In the root CMakeLists.txt
, I added SET_PROPERTY(GLOBAL PROPERTY GLOBAL_NAME "cproj")
. This created a global property named GLOBAL_NAME
.
And in the CMakeLists.txt
in test
directory, I use GET_PROPERTY(ROOT_PROJECT_NAME GLOBAL PROPERTY GLOBAL_NAME)
to store the value of the property in a local variable called ROOT_PROJECT_NAME
. Then I can access the value via that variable.
EDIT: According to @StoryTeller, I can just delete the declaration of new project in test/CMakeLists.txt
and use CMAKE_PROJECT_NAME
instead.
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
add a comment |
I found the solution and I guess I'll answer it myself.
In the root CMakeLists.txt
, I added SET_PROPERTY(GLOBAL PROPERTY GLOBAL_NAME "cproj")
. This created a global property named GLOBAL_NAME
.
And in the CMakeLists.txt
in test
directory, I use GET_PROPERTY(ROOT_PROJECT_NAME GLOBAL PROPERTY GLOBAL_NAME)
to store the value of the property in a local variable called ROOT_PROJECT_NAME
. Then I can access the value via that variable.
EDIT: According to @StoryTeller, I can just delete the declaration of new project in test/CMakeLists.txt
and use CMAKE_PROJECT_NAME
instead.
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
add a comment |
I found the solution and I guess I'll answer it myself.
In the root CMakeLists.txt
, I added SET_PROPERTY(GLOBAL PROPERTY GLOBAL_NAME "cproj")
. This created a global property named GLOBAL_NAME
.
And in the CMakeLists.txt
in test
directory, I use GET_PROPERTY(ROOT_PROJECT_NAME GLOBAL PROPERTY GLOBAL_NAME)
to store the value of the property in a local variable called ROOT_PROJECT_NAME
. Then I can access the value via that variable.
EDIT: According to @StoryTeller, I can just delete the declaration of new project in test/CMakeLists.txt
and use CMAKE_PROJECT_NAME
instead.
I found the solution and I guess I'll answer it myself.
In the root CMakeLists.txt
, I added SET_PROPERTY(GLOBAL PROPERTY GLOBAL_NAME "cproj")
. This created a global property named GLOBAL_NAME
.
And in the CMakeLists.txt
in test
directory, I use GET_PROPERTY(ROOT_PROJECT_NAME GLOBAL PROPERTY GLOBAL_NAME)
to store the value of the property in a local variable called ROOT_PROJECT_NAME
. Then I can access the value via that variable.
EDIT: According to @StoryTeller, I can just delete the declaration of new project in test/CMakeLists.txt
and use CMAKE_PROJECT_NAME
instead.
edited Jan 1 at 9:55
answered Jan 1 at 9:18
tjysdsgtjysdsg
5017
5017
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
add a comment |
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
If anybody have better solution, feel free to add!
– tjysdsg
Jan 1 at 9:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994016%2fcmake-get-root-project-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is there a reason why you need to access the root project name? Normally I just call the root project "xproject" and the tests project will be simply "xproject-test"
– Thu Yein Tun
Jan 1 at 8:36
@ThuYeinTun Because I may want to change the name in the future, and I prefer changing it only once while taking effect across the project
– tjysdsg
Jan 1 at 8:52