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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:14+00:00 2026-06-13T11:39:14+00:00

The project structure below is a simplified example. I tried to boil it down

  • 0

The project structure below is a simplified example. I tried to boil it down to the minimal amount of files to reproduce my issue.

.
├── CMakeLists.txt
├── subdir1
│   ├── CMakeLists.txt
│   └── subsubdir1
│       ├── CMakeLists.txt
│       ├── Example.cpp
│       └── Example.h
└── subdir2
    ├── CMakeLists.txt
    ├── main.cpp
    └── subsubdir1
        ├── CMakeLists.txt
        ├── ExampleCreator.cpp
        └── ExampleCreator.h

./CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(test)

macro(add_sources)
    file (RELATIVE_PATH _relPath "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
    foreach (_src ${ARGN})
        if (_relPath)
            list (APPEND SRCS "${_relPath}/${_src}")
        else()
            list (APPEND SRCS "${_src}")
        endif()
    endforeach()
    if (_relPath)
        # propagate SRCS to parent directory
        set (SRCS ${SRCS} PARENT_SCOPE)
    endif()
endmacro()

add_subdirectory(subdir1)
add_subdirectory(subdir2)

add_executable(test ${SRCS})

subdir1/CMakeLists.txt

add_subdirectory(subsubdir1)

subdir1/subsubdir1/CMakeLists.txt

add_sources(Example.cpp)

subdir1/subsubdir1/Example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

class Example
{
public:
    Example();
    virtual ~Example();
};

#endif

subdir1/subsubdir1/Example.cpp

#include <stdio.h>
#include "Example.h"

Example::Example()
{
    printf("Inside Example constructor\n");
}

Example::~Example()
{
}

subdir2/CMakeLists.txt

add_subdirectory(subsubdir1)
add_sources(main.cpp)

subdir2/main.cpp

#include "subsubdir1/ExampleCreator.h"

int main(int argc, char** argv)
{
    ExampleCreator creator;
    return 0;
}

subdir2/subsubdir1/CMakeLists.txt

add_sources(ExampleCreator.cpp)

subdir2/subsubdir1/ExampleCreator.h

#ifndef EXAMPLE_CREATOR_H
#define EXAMPLE_CREATOR_H

class ExampleCreator
{
public:
    ExampleCreator();
    virtual ~ExampleCreator();
};

#endif

subdir2/subsubdir1/ExampleCreator.cpp

#include "ExampleCreator.h"
#include "../../subdir1/subsubdir1/Example.h"

ExampleCreator::ExampleCreator()
{
    Example* ex1 = new Example();
}

ExampleCreator::~ExampleCreator()
{
}

I’m hoping this is a really simple lack of understanding of how CMake handles dependencies. This compiles without error, but fails during linking. The make output below shows that Example.cpp isn’t even compiling and I don’t understand why.

user>:~/src/test/build$ make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/subdir2/subsubdir1/ExampleCreator.cpp.o
[100%] Building CXX object CMakeFiles/test.dir/subdir2/main.cpp.o
Linking CXX executable test
CMakeFiles/test.dir/subdir2/subsubdir1/ExampleCreator.cpp.o: In function `ExampleCreator::ExampleCreator()':
ExampleCreator.cpp:(.text+0x2b): undefined reference to `Example::Example()'
collect2: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

All the sources are appended to the SRCS variable in the root CMakeLists.txt file from what I can tell. So, why isn’t Example.cpp getting compiled? or linked?

  • 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-13T11:39:15+00:00Added an answer on June 13, 2026 at 11:39 am

    The directory subdir1 doesn’t generate a binary. Add this:

    project(Example)
    add_library(Example Example.cpp)
    

    instead of your add_sources. After this, you need to tell the project that uses it to link against it:

    TARGET_LINK_LIBRARIES(subdir2 Example)
    

    If your names differ, document yourself on the functionalities of these commands.

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

Sidebar

Related Questions

I have a project structure like: src/CMakeLists.txt src/test/component1/CMakeLists.txt src/test/component2/CMakeLists.txt For the testing, I'm using
I am new to spring MVC. i have below project structure. Below are classes:
I have a xml file with below structure <project> <dependency id=abc version=1.2.3.4/> </project> I
I have download a sample project and saw the below package structure. src/main/java src/main/resources
Given the table structure below, how do you project x and y only with
I have this flex project.And the below mentioned is my directory structure (List1).Now if
Below is a simplified example that demonstrates the problem I am having. One hundred
Below you can see my current project structure. But I am not fully happy
I have a c++ project which directory structure like below: server/ code/ BASE/ Thread/
Below is structure of my Jar file root - template.ftl - org.project.myproject.App.java Inside App.java,

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.