I want to use GMock in my project. First of all I compiled GMock and GTest. The sequence of my actions (I use Linux):
git clone googlemock and googletest
go to GIT/googletest/googlemock/build-aux/ directory run cmake ..
and then make
As the result I got: libgmock.a and libgmock_main.a
My project has the structure: build, inc, src and lib directories. In build directory I run cmake .. and make. In inc dir I placed all headers from GIT/googletest/googlemock/include/ and GIT/googletest/googletest/include/. In lib dir lays libgmock.a only. In src - sources of my project.
My CMakeLists.txt contains:
project(blockchain)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB CPPS "*.cpp")
include_directories("../inc/")
link_directories("../lib/")
add_definitions(-Wall -O2 -std=c++11)
add_executable(${PROJECT_NAME} ${CPPS})
target_link_libraries(blockchain gmock)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
When I try to make my project (typing cmake .. && make in build dir) I get linking errors:
CMakeFiles/blockchain.dir/main.cpp.o: In function main':
main.cpp:(.text.startup+0x13): undefined reference
totesting::UnitTest::GetInstance()'
main.cpp:(.text.startup+0x1b): undefined reference to testing::UnitTest::Run()' ../lib/libgmock.a(gmock-all.cc.o): In
functiontesting::internal::scoped_ptr
::reset(std::__cxx11::basic_stringstream) [clone .part.81] [clone .constprop.367]':
gmock-all.cc:(.text+0x21a): undefined reference to testing::internal::IsTrue(bool)' ../lib/libgmock.a(gmock-all.cc.o): In
functiontesting::internal::MutexBase::AssertHeld() const [clone
.constprop.368]':
gmock-all.cc:(.text+0x274): undefined reference to testing::internal::IsTrue(bool)' gmock-all.cc:(.text+0x2b2): undefined
reference
totesting::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity,
char const, int)'
gmock-all.cc:(.text+0x2fd): undefined reference to testing::internal::GTestLog::~GTestLog()' gmock-all.cc:(.text+0x312):
undefined reference totesting::internal::GTestLog::~GTestLog()'
../lib/libgmock.a(gmock-all.cc.o): In function testing::internal::Expect(bool, char const*, int,
std::__cxx11::basic_string,
std::allocator > const&) [clone .part.51]':
gmock-all.cc:(.text+0x910): undefined reference
totesting::Message::Message()'
gmock-all.cc:(.text+0x926): undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)' gmock-all.cc:(.text+0x931): undefined
reference
totesting::internal::AssertHelper::operator=(testing::Message const&)
const'
gmock-all.cc:(.text+0x939): undefined reference to testing::internal::AssertHelper::~AssertHelper()'
gmock-all.cc:(.text+0x94b): undefined reference
totesting::internal::IsTrue(bool)'
And a lot of others linking errors. What am I doing wrong?
gmock depends on gtest. You added the former, but not the latter.
Related
I am trying to include some libraries i downloaded using vcpkg. I want to use all of them from vcpkg instead of system libraries.
I am able to run cmake .. command in build directory. It is successful.
Then i run make command but i am getting lots of "undefined reference to" error for all libraries i linked.
Here is CmakeLists.txt:
set(CMAKE_TOOLCHAIN_FILE "/home/ubuntu-user/Desktop/flight-simulator/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(PROJECT_NAME Simulator)
cmake_minimum_required(VERSION 3.12)
project(${PROJECT_NAME})
##OPENGL
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
##GLAD
find_path(GLAD_INCLUDE_DIR glad/glad.h)
find_library(GLAD_LIBRARY glad)
#message(${GLAD_INCLUDE_DIR})
#ASSIMP
find_library(ASSIMP_LIBRARY assimp)
#IMGUI
set(IMGUI_DIR ./libs/imgui-implot)
add_library(imgui STATIC
${IMGUI_DIR}/imgui_impl_glfw.cpp
${IMGUI_DIR}/imgui.cpp
)
#SOURCES
FILE(GLOB SOURCES
"src/Data.cpp"
"src/Frames.cpp"
"src/Model.cpp"
"src/main.cpp"
)
#EXECUTABLE
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${IMGUI_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${GLAD_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} imgui ${OPENGL_LIBRARIES} assimp ${GLAD_LIBRARY})
Sample error log:
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1535): undefined reference to `glfwSetCursor'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x154e): undefined reference to `glfwSetInputMode'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_UpdateGamepads()':
imgui_impl_glfw.cpp:(.text+0x15e8): undefined reference to `glfwGetGamepadState'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_NewFrame()':
imgui_impl_glfw.cpp:(.text+0x1bb5): undefined reference to `glfwGetWindowSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1bcf): undefined reference to `glfwGetFramebufferSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1c4a): undefined reference to `glfwGetTime'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiStyle::ImGuiStyle()':
imgui.cpp:(.text+0xe23): undefined reference to `ImGui::StyleColorsDark(ImGuiStyle*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImBezierCubicClosestPoint(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, int)':
imgui.cpp:(.text+0x2614): undefined reference to `ImBezierCubicCalc(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, float)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiTextFilter::Draw(char const*, float)':
imgui.cpp:(.text+0x564d): undefined reference to `ImGui::InputText(char const*, char*, unsigned long, int, int (*)(ImGuiInputTextCallbackData*), void*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper_SeekCursorAndSetupPrevLine(float, float)':
imgui.cpp:(.text+0x652f): undefined reference to `ImGui::TableEndRow(ImGuiTable*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper::Begin(int, float)':
I tried writing add_library instead of add_executable and make completed successfully. But then i don't have any executable output.
You are using imgui-plot which still requires ImGUI environment. ImGUI Plot Docs here. ImGUI example for GLFW3 and OpenGL3 here
You didn't link GLFW.
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw3 imgui assimp ${GLAD_LIBRARY})
I'm developing a dynamic C++17 library that uses SQLite. It's a CMake project and the requirement for it to be dynamic comes because I plan to mainly use the library through JNI.
So, in my project I build SQLite as a static library:
add_library(sqlite3 STATIC "${sqlite3_SOURCE_DIR}/sqlite3.c") # STATIC to efficiently link to SHARED main library
set_target_properties(sqlite3 PROPERTIES POSITION_INDEPENDENT_CODE ON) # Required to link STATIC to SHARED
target_include_directories(sqlite3 PUBLIC "${sqlite3_SOURCE_DIR}")
And my main library as shared:
add_library(${PROJECT_NAME} SHARED <library sources>)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) # To include headers without using relative paths
target_link_libraries(${PROJECT_NAME} PRIVATE sqlite3)
Then if I link my library to some executable like this it builds and runs absolutely fine on my PC:
add_executable(app <app's sources>)
target_compile_features(app PUBLIC cxx_std_17)
target_include_directories(app PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} <main library src dir>) # Including the main library like this is a temporary solution, because there is no separate include dir yet
target_link_libraries(app PUBLIC ${PROJECT_NAME})
But when I try to build it in GitHub Actions I get these errors:
[100%] Linking CXX executable app
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_create'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlopen'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlclose'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlerror'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlsym'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_join'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_init'
collect2: error: ld returned 1 exit status
I can fix it by linking the library target to ${CMAKE_DL_LIBS} and Threads::Threads, but I'm not sure if I'm doing what should be done and not just silencing some other mistakes of mine.
To be clear, I don't use any of the functions mentioned in the errors above in the code of my library.
The questions are:
Why does the original code work on my PC and doesn't on GitHub Actions?
Is what I did the right way to fix this?
I'm building the project locally on Ubuntu 22.04 (WSL) with a compiler identified as GNU 11.3.0, while GitHub Actions runs Ubuntu 20.04 using GNU 9.4.0 compiler.
I need help with linking opencv library to my clion project. I am trying to do this on Windows 10. I've tried to do following steps:- Install OpenCV from opencv.org in verssion 4.5.1- Add C:\OpenCV_4.5.1\build\x64\vc15\bin to system PATH variable- Add lines to CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(opencv)
set(CMAKE_CXX_STANDARD 17)
add_executable(opencv main.cpp)
set(OpenCV_DIR "C:\\OpenCV_4.5.1\\build\\x64\\vc15\\lib")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(opencv ${OpenCV_LIBS})
But the library still doesn't work. After compiling I'm getting these errors:
undefined reference to `cv::Mat::Mat()'
undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
undefined reference to `cv::waitKey(int)'
undefined reference to `cv::Mat::~Mat()'
undefined reference to `cv::VideoCapture::~VideoCapture()'
undefined reference to `cv::Mat::~Mat()'
I don't know how to solve this, I will be verry greatful for helping me. Thanks in advance.
This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I've just started to use CLion as my IDE after mainly using code::blocks on Linux for the last year or so. However I'm finding it really difficult to set up a project that worked and compiled fine in code::blocks because I just don't understand cmake yet.
Here's a quick description of my project - it consists of a main file that is calling a few header and source files, which all in the same directory. In the main file I'm also calling an external header file ("coupling.h", which is located in "/opt/package/API_SRC_Files/Coupling") which describes some classes relating to coupling to that software. The "coupling.h" file in turn references some files in "/opt/package/API_SRC_Files/Coupling". I also need to link to a shared library file "libcoupling.so" that is located in "/opt/package/lib".
Setting this up in code::blocks was pretty easy - just go to build options, and the respective paths to the search directories, and the path to the linked file and then build. Compiles in a few seconds.
I've tried to setup the project in CLion and CMake but I'm truly lost and I don't really understand why CMake is not finding the "coupling.h" file and throws countless "undefined reference to " errors. I'm sure I'll also have a problem setting up the shared library and I'm scared to even think about the difference in debug and release versions at the moment!
Here's my current CMake.txt file, hopefully someone can help. I'm using CLion 2020.1 on Fedora.
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
# add extra include directories
include_directories(.)
include_directories(/opt/package/API_SRC_Files)
include_directories(/opt/package/API_SRC_Files/Core)
include_directories(/opt/package/API_SRC_Files/Coupling)
set(PROJECT_HEADERS
coupling_utilities.h
geometry.h
io.h
shapelib.h
pid.h
spline.h
)
set(PROJECT_SOURCES
main.cpp
io.cpp
coupling_utilities.cpp
shapelib.cpp
pid.cpp
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(MBD ${PROJECT_SOURCES} ${PROJECT_HEADERS})
Here's the error log from the output related to the include_directories():
/snap/clion/114/bin/cmake/linux/bin/cmake --build /home/user/CLionProjects/mbd/cmake-build-debug --target mbd -- -j 24
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/CLionProjects/mbd/cmake-build-debug
[ 16%] Linking CXX executable mbd
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `main':
/home/user/CLionProjects/mbd/main.cpp:224: undefined reference to `NApi::Coupling::getGeometryId(char const*, int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:260: undefined reference to `NApi::Coupling::getTimeStep(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:268: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:273: undefined reference to `NApi::Coupling::setTime(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:276: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:285: undefined reference to `NApi::Coupling::setGridCellSize(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:288: undefined reference to `NApi::Coupling::setNumberOfCores(int const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:343: undefined reference to `NApi::Coupling::getGeometryPosition(int, NApi::C3dValue&, NApi::C3x3Matrix&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:345: undefined reference to `NApi::Coupling::getGeometryTranslation(int, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:348: undefined reference to `NApi::Coupling::getGeometryVelocity(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:442: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:444: undefined reference to `NApi::Coupling::getGeometryForces(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:531: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:534: undefined reference to `NApi::Coupling::setGeometryMotion(int, NApi::C3dValue const&, NApi::C3x3Matrix const&, NApi::C3dValue const&, NApi::C3dValue const&, double, bool)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:554: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:556: undefined reference to `NApi::Coupling::simulate(double const&, double)'
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::ICoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::~ICoupling()'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `connect(NApi::Coupling&)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:54: undefined reference to `NApi::Coupling::initialiseCoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:67: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:90: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `getResumeTimestep(NApi::Coupling&, double, double)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:109: undefined reference to `NApi::Coupling::getNumberOfTimesteps(unsigned int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:136: undefined reference to `NApi::Coupling::getTimesteps(double*, unsigned int&, unsigned int)'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/mbd.dir/build.make:144: mbd] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/mbd.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mbd.dir/rule] Error 2
gmake: *** [Makefile:118: mbd] Error 2
Here is how I would write the cmakelists.txt file:
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
set(API_SRC_Files /opt/package/API_SRC_Files) # ${API_SRC_Files}
set(SRC_FILES
main.cpp
coupling_utilities.h coupling_utilities.cpp
geometry.h
io.h io.cpp
pid.h pid.cpp
shapelib.h shapelib.cpp
spline.h
)
# add extra include directories
include_directories(
includes
.
${API_SRC_Files}
${API_SRC_Files}/Core
${API_SRC_Files}/Coupling
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(${PROJECT_NAME} ${SRC_FILES})
Use something like the API_SRC_Files variable for two reasons: reduces errors from having to repeat the same path information, makes it easier if the library is ever moved to a new location. The same thing with using the PROJECT_NAME variable. The comment is to remind myself how to utilize the variable later on in the file.
Combine your SRC_FILES and SRC_HEADERS into one array with source and matching header on the same line. This helps to make sure both are listed. Please note when you tell Clion you want to add a new C++ Class, it will be in the form class.cpp class.h, and not alphabetized. I always list the main.cpp, first.
The dot, ., in the include_directories may or may not be necessary.
Hope this helps.
I'm trying to create a Node.js addon in C++ following the example provided in this guide.
I've linked the node.h and v8.h libraries setting my CMakeLists.txt in this way:
cmake_minimum_required(VERSION 3.6) project(node___C__)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(node___C__ ${SOURCE_FILES})
include_directories(/usr/include/nodejs/src)
include_directories(/usr/include/nodejs/deps/v8/include)
I think libraries are correctly setted in my CMakeList.txt but when I try to compile the file in Clion I get this error:
/usr/include/nodejs/src/node.h:239: undefined reference to `v8::Isolate::GetCurrent()'
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::HandleScope(v8::Isolate*)'
/usr/include/nodejs/src/node.h:242: undefined reference to `v8::FunctionTemplate::New(v8::Isolate*, void (*)(v8::FunctionCallbackInfo<v8::Value> const&), v8::Local<v8::Value>, v8::Local<v8::Signature>, int)'
/usr/include/nodejs/src/node.h:243: undefined reference to `v8::FunctionTemplate::GetFunction()'
/usr/include/nodejs/src/node.h:244: undefined reference to `v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int)'
/usr/include/nodejs/src/node.h:245: undefined reference to `v8::Function::SetName(v8::Local<v8::String>)'
/usr/include/nodejs/src/node.h:246: undefined reference to `v8::Object::Set(v8::Local<v8::Value>, v8::Local<v8::Value>)'
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::~HandleScope()'
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::~HandleScope()'
What I'm doing wrong? I'm quite new with C++ so maybe I'm doing some dumb mistake.
I've forgot to link some dependencies?