我正在学习cmake,但是在构建自己的stl时遇到了问题。
我使用cmake构建项目。
这是我的项目树。
这是我的根CMakeLists:
//CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (Demo3)
add_subdirectory(math)
aux_source_directory(. DIR_SRCS)
add_executable(Demo ${DIR_SRCS})
target_link_libraries(Demo MathFunctions)
以及子目录CMakeLists.txt:
//math/CMakeLists.txt
aux_source_directory(. DIR_LIB_SRCS)
add_library (MathFunctions ${DIR_LIB_SRCS})
但是当我在构建目录中运行cmake ..
和make
时,出现以下错误:
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/test.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cpp.o
[100%] Linking CXX executable Demo
CMakeFiles/Demo.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x49): undefined reference to `power(double, double)'
collect2: error: ld returned 1 exit status
CMakeFiles/Demo.dir/build.make:84: recipe for target 'Demo' failed
make[2]: *** [Demo] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/Demo.dir/all' failed
make[1]: *** [CMakeFiles/Demo.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
我认为我已经建立了libMathFunctions.a
,但它怎么说undefined reference
?
我不知道发生了什么,我整天都在互联网上搜索。
有人可以告诉我哪里出了问题吗?
非常感谢您!
//test.h
double power(double base, int exponent);
//test.cpp
#include "test.h"
double power(double base, int exponent)
{
int result = base;
int i;
if (exponent == 0) {
return 1;
}
for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
//main.cpp
#include <iostream>
#include <string>
#include "math/test.h"
int main()
{
double base ;
int exponent ;
std::cin >> base >> exponent;
double result = power(base, exponent);
std::cout << base << " ^ " << exponent << " = " << result << std::endl;
return 0;
}