Been having a little issue figuring out how to link things together with CMake and I've been stuck for three days so far. No matter what I've tried I've been having an issue in having the function returnThisClass()
being undefined no matter what I've tried.
On the minimum reproducible example in headertest.cpp
, there is an undefined reference to the creation of the pointer, the same error is not present on my program despite everything appearing to be a direct copy.
My file structure is
main.cpp
src/
headertest.cpp
otherheader.cpp
lib/
headertest.h
otherheader.h
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(stackoverflowExample1 VERSION 0.1.0)
include(CTest)
enable_testing()
add_library(aTest STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/otherheader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/headertest.cpp)
target_include_directories(aTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/)
set(
SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/headertest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/otherheader.cpp
)
add_executable(stackoverflowExample1 ${SOURCE_FILES})
target_link_libraries(stackoverflowExample1 PRIVATE aTest)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
main.cpp
#include "lib/otherheader.h"
int main() {
doSomethingHere();
}
headertest.h
namespace theTest {
class foo1 {
public:
foo1();
};
foo1 * returnThisClass();
}
otherheader.h
void doSomethingHere();
headertest.cpp
#include "../lib/headertest.h"
theTest::foo1 * returnThisClass(){
theTest::foo1 * aFoo1 = new theTest::foo1();
return aFoo1;
}
otherheader.cpp
#include "../lib/otherheader.h"
#include "../lib/headertest.h"
void doSomethingHere(){
theTest::foo1 *foo1Ptr = theTest::returnThisClass();
}