Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:36:28+00:00 2026-06-12T19:36:28+00:00

Suppose my project’s CMakeLists.txt includes foo.cmake : include(foo) In foo.cmake , i want to

  • 0

Suppose my project’s CMakeLists.txt includes foo.cmake:

include(foo)

In foo.cmake, i want to know the path of foo.cmake.

How can I do that?

Note that CMAKE_CURRENT_LIST_DIR gives the directory of the including CMakeLists.txt, not that of the included foo.cmake, and is thus not what I want.

Of course, foo.cmake might be included by several projects (i.e., by several CMakeLists.txt files).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:36:29+00:00Added an answer on June 12, 2026 at 7:36 pm

    People have reported seemingly contradictory facts about how CMAKE_CURRENT_LIST_DIR behaves. Now I know the reason for the confusion:

    First, in my Linux environment:

    $ cd /path/to/home  
    $ mkdir cmake-test  
    $ cd cmake-test  
    $ mkdir source  
    $ mkdir source/subdirectory  
    $ mkdir build  
    

    I create these two files:

    $ cat source/CMakeLists.txt  
    include(subdirectory/foo.cmake)  
    
    $ cat source/subdirectory/foo.cmake  
    message("CMAKE_CURRENT_LIST_DIR is ${CMAKE_CURRENT_LIST_DIR}")  
    

    CMake works as reported by Fraser and Robert Dailey:

    $ cd build  
    $ cmake ../source  
    CMAKE_CURRENT_LIST_DIR is /path/to/home/cmake-test/source/subdirectory  
    [...]  
    

    However, I add a function to foo.cmake, which I call from CMakeLists.txt:

    $ cat ../source/subdirectory/foo.cmake  
    message("CMAKE_CURRENT_LIST_DIR is ${CMAKE_CURRENT_LIST_DIR}")  
    function(bar)  
        message("CMAKE_CURRENT_LIST_DIR in bar() is ${CMAKE_CURRENT_LIST_DIR}")  
    endfunction()  
    
    $ cat ../source/CMakeLists.txt  
    include(subdirectory/foo.cmake)  
    bar()  
    

    Then:

    $ cmake ../source  
    CMAKE_CURRENT_LIST_DIR is /path/to/home/cmake-test/source/subdirectory  
    CMAKE_CURRENT_LIST_DIR in bar() is /path/to/home/cmake-test/source  
    [...]  
    

    So, the value of CMAKE_CURRENT_LIST_DIR in foo.cmake is not the same at the time foo.cmake is included and when bar() is called. This is according to the specification of CMAKE_CURRENT_LIST_DIR.

    Here is one possible solution for accessing the directory of foo.cmake from within bar():

    $ cat ../source/subdirectory/foo.cmake  
    set(DIR_OF_FOO_CMAKE ${CMAKE_CURRENT_LIST_DIR})  
    function(bar)  
        message("DIR_OF_FOO_CMAKE in bar() is ${DIR_OF_FOO_CMAKE}")  
    endfunction()  
    

    after which I get the behavior I was looking for:

    $ cmake ../source  
    DIR_OF_FOO_CMAKE in bar() is /path/to/home/cmake-test/source/subdirectory  
    [...]  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I know that a file of a certain name is in a project
Suppose you want to throw Exception like this: 'Project with the provided ID cannot
Suppose, I have an opensource project that depends on some library, that must be
I am using buildr to build my scala project and suppose I want to
In a project I cannot find one file. I suppose that file was deleted
Suppose I have a project MyFramework that has some code, which is used across
Suppose you have an existing large project and you want to integrate Code Contracts
Suppose we have a user library project ProjectUserlib that uses an external jar external.jar,
Suppose that I host a project in some open source project hosting services like
So suppose I have two Models -- Project and Task I want user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.