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

  • SEARCH
  • Home
  • 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 8694035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:41:10+00:00 2026-06-13T00:41:10+00:00

I’m building a project whose final output is a static library, and my CMake-based

  • 0

I’m building a project whose final output is a static library, and my CMake-based build system consists of two subdirectories – the Src and the Tests – where the build for the tests produces an executable and links the to the library which is built from src.

My problem is that the test build requires the library to already exist if it is to proceed without any errors. Is there a way to get CMake to understand that the library will exists when it comes to build the tests, or do I have to do these in separate steps?

My CMakeLists.txt files as as follows:

Root file:

cmake_minimum_required( VERSION 2.8 )
project( mylib )
add_subdirectory( Src )
add_subdirectory( Tests )

Src file:

file( GLOB MYLIB_SOURCES *.cpp )
add_library( mylib ${MYLIB_SOURCES} )

Test file:

file( GLOB MYLIB_TESTS *.cpp )
add_executable( tests ${MYLIB_TESTS} )

find_package( GTest REQUIRED )
find_library( LIB_MYLIB NAMES mylib PATHS "${CMAKE_SOURCE_DIR}/Build/Src" )

include_directories( ../Src )
include_directories( ${GTEST_INCLUDE_DIRECTORIES} )

target_link_libraries( tests ${LIB_MYLIB} ${GTEST_LIBRARIES} pthread )
  • 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-13T00:41:11+00:00Added an answer on June 13, 2026 at 12:41 am

    CMake should be able to figure out the dependency between Src and Tests automatically, provided of course you call CMake only on your root CMakeLists.txt. You don’t really need a find_library.

    So, I would keep your Src CMakeLists.txt as follows: For increased ‘encapsulation’ you could e.g. set ‘MyLib_INCLUDE_DIRS’ there and force it into the cache:

    project( MyLib )
    file( GLOB MYLIB_SOURCES *.cpp )
    add_library( mylib ${MYLIB_SOURCES} )
    # I do not know
    set( mylib_INCLUDE_DIRS ${MyLib_SOURCE_DIR} CACHE STRING "Include-directories for MyLib" FORCE )
    

    and rewrite your Tests CMakeLists.txt:

    project( MyTests )
    file( GLOB MYLIB_TESTS *.cpp )
    add_executable( tests ${MYLIB_TESTS} )
    
    find_package( GTest REQUIRED )
    include_directories( ${mylib_INCLUDE_DIRS} )
    include_directories( ${GTEST_INCLUDE_DIRECTORIES} )
    
    target_link_libraries( tests mylib ${GTEST_LIBRARIES} pthread )
    

    If you wish to build “tests” only, I suggest you call CMake on the root CMakeLists.txt and then step into the tests directory and call ‘make’ or ‘msbuild’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.