Cmake:对包含方法的未定义引用
Cmake: undefined reference to included methods
我正在学习使用 cmake,我正在尝试使用 gtest 为我编写的一个非常小的项目编译一组简单的测试。
我的CMakeLists.txt长得像
cmake_minimum_required(VERSION 2.6)
project(circuit_sim)
include(FetchContent)
FetchContent_Declare(
googletest
# Specify the commit you depend on and update it regularly.
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(test Connector.cpp test.cpp)
target_link_libraries(test gtest_main)
我从 googletest 文档中获得了大部分内容。我正在尝试编译一个在 test.cpp 中具有 main() 并依赖于 Connector.cpp 和 Connector.h 中的连接器 class 的可执行文件。所有文件都在同一目录中
当我 运行 cmake 时。然后让我得到以下错误:
/usr/bin/ld: CMakeFiles/test.dir/test.cpp.o: in function `AllTests_CircuitTest_Test::TestBody()':
test.cpp:(.text+0x33): undefined reference to `Connector::Connector()'
/usr/bin/ld: test.cpp:(.text+0x4c): undefined reference to `Connector::Connector()'
/usr/bin/ld: test.cpp:(.text+0x6d): undefined reference to `Connector::Connector(Connector*)'
/usr/bin/ld: test.cpp:(.text+0x7d): undefined reference to `Connector::connect(Connector*)'
/usr/bin/ld: test.cpp:(.text+0x93): undefined reference to `Connector::in(unsigned long, unsigned long)'
/usr/bin/ld: test.cpp:(.text+0xb8): undefined reference to `Connector::out()'
/usr/bin/ld: test.cpp:(.text+0xc4): undefined reference to `Connector::get_out_conn()'
/usr/bin/ld: test.cpp:(.text+0xd6): undefined reference to `Connector::get_v()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:86: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:139: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
尽管我在CMakeLists.txt中指定了cmake,但似乎并不知道要在编译中包含Connector.cpp。我做错了什么?
Connector.cpp 的代码,目前它的功能并不多,哈哈,但它是一个 wip
#include "Connector.h"
#include "circuit_utils.h"
Connector::Connector(){
this->v = 0;
this->c = 0;
this->out_conn = nullptr;
}
Connector::Connector(Connector* out_conn){
this->v = 0;
this->c = 0;
this->out_conn = out_conn;
}
Connector* Connector::connect(Connector* out_conn){
this->out_conn = out_conn;
return this;
}
Connector* Connector::in(uint64_t v, uint64_t c){
this->v = v;
this->c = c;
//this->out();
return this;
}
Connector* Connector::out(){
if(out_conn != nullptr)
out_conn->in(v, c);
return this;
}
uint64_t Connector::get_v() { return v; }
uint64_t Connector::get_c() { return c; }
Connector* Connector::get_out_conn() { return out_conn; }
std::string Connector::to_string() {
std::string to_return = "";
to_return += "-------------\n";
to_return += "voltage: " + std::to_string(FROM_MICROS(((double)v))) + " volts\n";
to_return += "current: " + std::to_string(FROM_MICROS(((double)c))) + " amps\n";
to_return += "-------------\n";
return to_return;
}
test.cpp
的代码
#include <iostream>
#include <unistd.h>
#include "gtest/gtest.h"
#include "Connector.h"
#include "circuit_utils.h"
using namespace std;
TEST (AllTests, CircuitTest) {
Connector* power = new Connector();
Connector* ground = new Connector();
power->connect(new Connector(ground));
power->in(TO_MICROS(3.3), TO_MICROS(3.3));
Connector *cur_conn = power;
while(cur_conn != nullptr){
sleep(5);
cur_conn->out();
cur_conn = cur_conn->get_out_conn();
}
ASSERT_EQ(3.3, ground->get_v());
}
既然你需要外部class,你应该把那个class“库”放在target_link_libraries()
中,这样你的CMakeLists.txt的最后一行就会变成
target_link_libraries(test connector gtest_main)
此外,在与 Connector.cpp 相同目录中的另一个 CMakeLists.txt 中,您应该将它们声明为一个库:
set(SOURCES Connector.cpp)
add_library(connector STATIC ${SOURCES})
我正在学习使用 cmake,我正在尝试使用 gtest 为我编写的一个非常小的项目编译一组简单的测试。
我的CMakeLists.txt长得像
cmake_minimum_required(VERSION 2.6)
project(circuit_sim)
include(FetchContent)
FetchContent_Declare(
googletest
# Specify the commit you depend on and update it regularly.
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(test Connector.cpp test.cpp)
target_link_libraries(test gtest_main)
我从 googletest 文档中获得了大部分内容。我正在尝试编译一个在 test.cpp 中具有 main() 并依赖于 Connector.cpp 和 Connector.h 中的连接器 class 的可执行文件。所有文件都在同一目录中
当我 运行 cmake 时。然后让我得到以下错误:
/usr/bin/ld: CMakeFiles/test.dir/test.cpp.o: in function `AllTests_CircuitTest_Test::TestBody()':
test.cpp:(.text+0x33): undefined reference to `Connector::Connector()'
/usr/bin/ld: test.cpp:(.text+0x4c): undefined reference to `Connector::Connector()'
/usr/bin/ld: test.cpp:(.text+0x6d): undefined reference to `Connector::Connector(Connector*)'
/usr/bin/ld: test.cpp:(.text+0x7d): undefined reference to `Connector::connect(Connector*)'
/usr/bin/ld: test.cpp:(.text+0x93): undefined reference to `Connector::in(unsigned long, unsigned long)'
/usr/bin/ld: test.cpp:(.text+0xb8): undefined reference to `Connector::out()'
/usr/bin/ld: test.cpp:(.text+0xc4): undefined reference to `Connector::get_out_conn()'
/usr/bin/ld: test.cpp:(.text+0xd6): undefined reference to `Connector::get_v()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:86: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:139: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
尽管我在CMakeLists.txt中指定了cmake,但似乎并不知道要在编译中包含Connector.cpp。我做错了什么?
Connector.cpp 的代码,目前它的功能并不多,哈哈,但它是一个 wip
#include "Connector.h"
#include "circuit_utils.h"
Connector::Connector(){
this->v = 0;
this->c = 0;
this->out_conn = nullptr;
}
Connector::Connector(Connector* out_conn){
this->v = 0;
this->c = 0;
this->out_conn = out_conn;
}
Connector* Connector::connect(Connector* out_conn){
this->out_conn = out_conn;
return this;
}
Connector* Connector::in(uint64_t v, uint64_t c){
this->v = v;
this->c = c;
//this->out();
return this;
}
Connector* Connector::out(){
if(out_conn != nullptr)
out_conn->in(v, c);
return this;
}
uint64_t Connector::get_v() { return v; }
uint64_t Connector::get_c() { return c; }
Connector* Connector::get_out_conn() { return out_conn; }
std::string Connector::to_string() {
std::string to_return = "";
to_return += "-------------\n";
to_return += "voltage: " + std::to_string(FROM_MICROS(((double)v))) + " volts\n";
to_return += "current: " + std::to_string(FROM_MICROS(((double)c))) + " amps\n";
to_return += "-------------\n";
return to_return;
}
test.cpp
的代码#include <iostream>
#include <unistd.h>
#include "gtest/gtest.h"
#include "Connector.h"
#include "circuit_utils.h"
using namespace std;
TEST (AllTests, CircuitTest) {
Connector* power = new Connector();
Connector* ground = new Connector();
power->connect(new Connector(ground));
power->in(TO_MICROS(3.3), TO_MICROS(3.3));
Connector *cur_conn = power;
while(cur_conn != nullptr){
sleep(5);
cur_conn->out();
cur_conn = cur_conn->get_out_conn();
}
ASSERT_EQ(3.3, ground->get_v());
}
既然你需要外部class,你应该把那个class“库”放在target_link_libraries()
中,这样你的CMakeLists.txt的最后一行就会变成
target_link_libraries(test connector gtest_main)
此外,在与 Connector.cpp 相同目录中的另一个 CMakeLists.txt 中,您应该将它们声明为一个库:
set(SOURCES Connector.cpp)
add_library(connector STATIC ${SOURCES})