I'm quite new to CMake and I'm having some trouble getting it working the way I want.
My Project structure:
.
├── CMakeLists.txt
├── src/
| ├── main.c
| ├── main.h
| └── CMakeLists.txt
├── include/
| └── mylib.h
├── test/
| ├── test.cpp
| ├── CMakeLists.txt
| └── CMakeLists.txt.in
└── build/
What I want to do is to build a shared library mylib
and then link it against a test target example
, however it doesn't seem to link mylib
agains the target example
.
What I do is going in to the build/
directory and do cmake ..
which runs fine. I then proceed to make
and run in to the problem:
test.cpp:(.text+0x27): undefined reference to `add(int, int)'
Down below is my files, the CMakeLists.in.txt
is for pulling gtest
which is working fine.
Cmake file in src/
set(SRC_FILES
main.c
main.h)
add_library(mylib SHARED ${SRC_FILES})
Cmake file in test/
...
add_executable(example test.cpp)
target_link_libraries(example
mylib
gtest_main)
add_test(NAME example_test COMMAND example)
(Gtest CMake code is omitted as that is not the problem)
CMake file in .
cmake_minimum_required(VERSION 2.8.2)
project(cmake_hello)
enable_testing()
add_subdirectory(src)
add_subdirectory(test)
mylib.h
#ifndef LIB_HEADER_H_
#define LIB_HEADER_H_
#include "../src/main.h"
#endif
main.c
#include "main.h"
int add (int a, int b)
{
return a + b;
}
main.h
#ifndef MAIN_H_
#define MAIN_H_
int add(int a, int b);
#endif
test.cpp
#include "gtest/gtest.h"
#include "../include/mylib.h"
TEST(example_test, test)
{
int a = add(1,2);
EXPECT_EQ(a,3);
}