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 9097043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:00:47+00:00 2026-06-17T00:00:47+00:00

I have a simple project going that uses CMake and gtest. I have a

  • 0

I have a simple project going that uses CMake and gtest. I have a basic CMakeLists.txt file working, but I want to get a better understanding of how to use multiple CMakeLists.txt’s and connect them. The code so far for the project is like this:

https://github.com/dmonopoly/writeart/tree/10b62048e6eb6a6ddd0658123d85ce4f5f601178

For quicker reference, the only CMakeLists.txt file (in the project root) that I take advantage of has this inside:

cmake_minimum_required(VERSION 2.8)

# Options
option(TEST "Build all tests." OFF) # makes boolean 'TEST' available

# Make PROJECT_SOURCE_DIR, PROJECT_BINARY_DIR, and PROJECT_NAME available
set(PROJECT_NAME MyProject)
project(${PROJECT_NAME})

set(CMAKE_CXX_FLAGS "-g") # -Wall")

#set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include) if you want your own include/ directory
# then you can do include_directories(${COMMON_INCLUDES}) in other cmakelists.txt files

################################
# Normal Libraries & Executables
################################
add_library(standard_lib Standard.cpp Standard.h)
add_library(converter_lib Converter.cpp Converter.h)
add_executable(main Main.cpp)

target_link_libraries(main standard_lib converter_lib)

################################
# Testing
################################
if (TEST)
    # This adds another subdirectory, which has project(gtest)
    add_subdirectory(lib/gtest-1.6.0)

    enable_testing()

    # Include the gtest library
    # gtest_SOURCE_DIR is available due to project(gtest) above
    include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

    ##############
    # Unit Tests
    ##############
    # Naming
    set(UNIT_TESTS runUnitTests)

    add_executable(${UNIT_TESTS} ConverterTest.cpp)

    # standard linking to gtest stuff
    target_link_libraries(${UNIT_TESTS} gtest gtest_main)

    # extra linking for the project
    target_link_libraries(${UNIT_TESTS} standard_lib converter_lib)

    # This is so you can do 'make test' to see all your tests run, instead of manually running the executable runUnitTests to see those specific tests.
    add_test(NAME myUnitTests COMMAND runUnitTests)
endif()

My goal is to move Standard.cpp and Standard.h into lib/. The moment I do this, though, I find the ordering of what I do in my CMakeLists.txt complicated. I need the library for my gtest setup, but the library would have to get made in lib/CMakeLists.txt, right? Wouldn’t it easily become really complicated to find where all your libraries are and executables are as you would have to look through all your CMakeLists.txt’s?

If I am missing something conceptually, or if there is a good example I could use to solve this easily, that would be great.

Help appreciated, and thanks in advance.

  • 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-17T00:00:48+00:00Added an answer on June 17, 2026 at 12:00 am

    If you don’t want to use multiple CMakeLists.txt files, don’t.

    ################################
    # Normal Libraries & Executables
    ################################
    
    add_library(standard_lib lib/Standard.cpp lib/Standard.h)
    add_library(converter_lib lib/Converter.cpp lib/Converter.h)
    
    # Main.cpp needs to know where "Standard.h" is for the #include, 
    #   so we tell it to search this directory too. 
    include_directories(lib)
    

    If you do want multiple CMakeLists.txt, you’d move it out:

    # Main CMakeLists.txt:
    add_subdirectory(lib)
    
    include_directories (${standard_lib_SOURCE_DIR}/standard_lib) 
    
    link_directories (${standard_lib_BINARY_DIR}/standard_lib) 
    

    and in /lib/CMakeLists.txt:

    add_library (standard_lib Standard.cpp)
    

    Here’s an example.

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

Sidebar

Related Questions

Currently I have a simple maven project that is building a jar file and
I have a project that I am working on for some simple CSS buttons,
I have a simple Scala project that runs without any problems inside Eclipse, however,
I have a very simple Setup project that copies three dlls into the GAC.
We have created a simple wix project for a basic windows application. Everything builds
I am working on a simple core data app that uses a tableview and
I have a Maven project that uses the jaxb2-maven-plugin to compile some xsd files.
I'd like to try out the D programming language. I have simple pet project
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I have a simple java project (adapted from the example here ), which is

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.