CMake: Get root project name












1















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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















1














If I gather your use case correctly, you should probably do as follows:




  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.


  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.







share|improve this answer


























  • 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











  • I made test standalone, I just realized that's a stupid move

    – tjysdsg
    Jan 1 at 9:48



















0














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.






share|improve this answer


























  • If anybody have better solution, feel free to add!

    – tjysdsg
    Jan 1 at 9:18











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
});


}
});














draft saved

draft discarded


















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









1














If I gather your use case correctly, you should probably do as follows:




  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.


  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.







share|improve this answer


























  • 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











  • I made test standalone, I just realized that's a stupid move

    – tjysdsg
    Jan 1 at 9:48
















1














If I gather your use case correctly, you should probably do as follows:




  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.


  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.







share|improve this answer


























  • 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











  • I made test standalone, I just realized that's a stupid move

    – tjysdsg
    Jan 1 at 9:48














1












1








1







If I gather your use case correctly, you should probably do as follows:




  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.


  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.







share|improve this answer















If I gather your use case correctly, you should probably do as follows:




  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.


  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 9:51

























answered Jan 1 at 9:43









StoryTellerStoryTeller

101k12204273




101k12204273













  • 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











  • 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











  • @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

















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













0














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.






share|improve this answer


























  • If anybody have better solution, feel free to add!

    – tjysdsg
    Jan 1 at 9:18
















0














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.






share|improve this answer


























  • If anybody have better solution, feel free to add!

    – tjysdsg
    Jan 1 at 9:18














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$