The different repository images
add_executable(test_labyrinthe test_labyrinthe.cpp)
add_test(test_labyrinthe.cpp test_labyrinthe)
target_link_libraries(test_labyrinthe ${GTEST_LIBRARIES})
This is what I have in Test/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(TP1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
Labyrinthe.cpp
Labyrinthe.h
Piece.cpp
Piece.h
Porte.cpp
Porte.h
Principal.cpp)
add_executable(TP1 ${SOURCE_FILES})
###########
## GTEST ##
###########
option(BUILD_TESTING "build unit tests" ON)
if(BUILD_TESTING)
include(ExternalProject)
ExternalProject_add(gtest-target
GIT_REPOSITORY "https://github.com/google/googletest"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/extern"
UPDATE_COMMAND ""
)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/extern/include)
link_directories(${CMAKE_CURRENT_BINARY_DIR}/extern/lib)
set(GTEST_LIBRARIES pthread gtest gmock gtest_main gmock_main)
enable_testing()
add_subdirectory(test)
endif()
This is what I have in the main CMakeLists.txt. Keep in mind that I have the only line #include <gtest/gtest.h> in test_labyrinthe.cpp.
When I run cmake .. and make, I got
[ 53%] Built target gtest-target
[ 86%] Built target TP1
Scanning dependencies of target test_labyrinthe
[ 93%] Building CXX object test/CMakeFiles/test_labyrinthe.dir/test_labyrinthe.cpp.o
[100%] Linking CXX executable test_labyrinthe
/usr/bin/ld: /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1/extern/lib/libgtest_main.a(gtest_main.cc.o): in function `main':
gtest_main.cc:(.text+0x3a): undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1/extern/lib/libgtest_main.a(gtest_main.cc.o): in function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x9): undefined reference to `testing::UnitTest::GetInstance()'
/usr/bin/ld: gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x11): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/test_labyrinthe.dir/build.make:84: test/test_labyrinthe] Error 1
make[1]: *** [CMakeFiles/Makefile2:152: test/CMakeFiles/test_labyrinthe.dir/all] Error 2
make: *** [Makefile:95: all] Error 2
This is really weird. Not sure how to make this work. Can you help?
EDIT When I run make test, I got
└╼ make test
Running tests...
Test project /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1
Start 1: test_labyrinthe.cpp
Could not find executable test_labyrinthe
Looked in the following places:
test_labyrinthe
test_labyrinthe
Release/test_labyrinthe
Release/test_labyrinthe
Debug/test_labyrinthe
Debug/test_labyrinthe
MinSizeRel/test_labyrinthe
MinSizeRel/test_labyrinthe
RelWithDebInfo/test_labyrinthe
RelWithDebInfo/test_labyrinthe
Deployment/test_labyrinthe
Deployment/test_labyrinthe
Development/test_labyrinthe
Development/test_labyrinthe
Unable to find executable: test_labyrinthe
1/1 Test #1: test_labyrinthe.cpp ..............***Not Run 0.00 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.00 sec
The following tests FAILED:
1 - test_labyrinthe.cpp (Not Run)
Errors while running CTest
make: *** [Makefile:84: test] Error 8
I believe the underlying problem is:
Keep in mind that I have the only line #include <gtest/gtest.h> in test_labyrinthe.cpp.
You'll want to add an actual test case to the file.
test_labyrinthe.cpp:
#include <gtest/gtest.h>
TEST(SomeTest, ABasicTest) {
EXPECT_EQ(1, 1);
}
Once you do that you'll probably run into some pthread linker errors. Changing to link pthread after gtest should resolve that.
set(GTEST_LIBRARIES gtest gmock gtest_main gmock_main pthread)
After uncountable tries, the solution was to add -pthread as you see here:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
Related
I'm trying to compile and execute a simple test program with curl lib.
#include <curl/curl.h>
using namespace std;
int main(){
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_cleanup();
return 0;
}
I have a CMakeLists to generate MakeFile :
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(ONLY25)
file(GLOB_RECURSE SRCS source/*.cpp)
file(GLOB_RECURSE HDRS source/*.h)
set(EXECUTABLE_NAME "ONLY25")
include_directories("C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
set(CURL_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw")
set(CURL_LIBRARY "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.a" "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a")
set(CURL_INCLUDE_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
find_package(CURL REQUIRED)
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
First, I use :
cmake -G "MinGW Makefiles" .
Everything works well
After that, I use :
C:\MinGW\bin\mingw32-make.exe
And I've got this linking error :
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x16): undefined reference to `_imp__curl_global_init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1d): undefined reference to `_imp__curl_global_cleanup'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\ONLY25.dir\build.make:106: recipe for target 'ONLY25.exe' failed
mingw32-make[2]: *** [ONLY25.exe] Error 1
CMakeFiles\Makefile2:93: recipe for target 'CMakeFiles/ONLY25.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/ONLY25.dir/all] Error 2
Makefile:101: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
I searched a lot on internet, there are some other posts on this topics but I think I did like they said.
I tried with different version of curl, and I'm using MinGW on Windows 10.
I'm really sorry if it's a dumb mistake, I still have a little trouble with Cmake. I'm stuck here for the whole day...
I'd appreciate your help with some explaination to not reproduce this aberration :)
I tried some new things thanks to responses :
set up FindCURL.cmake by copying this one link
change the CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
message(${CMAKE_MODULE_PATH})
set(CURL_LIBRARY "-lcurl")
find_package(CURL)
message(${CURL_LIBRARIES})
message(${CURL_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
Currently, the cmake command is working well and the variables seems legit:
CURL_LIBRARIES : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.aC:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a
CURL_INCLUDE_DIRS : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include
However, there is again the linking error with the make command...
I have a very small project to which I created some unit testing with GTest.
To build it, I'm using CMake. It compiles just fine, but it's giving me the error shown below. I include all make output just in case.
Scanning dependencies of target trace
[ 33%] Building C object CMakeFiles/trace.dir/trace/system_trace.c.o
[ 33%] Built target trace
Scanning dependencies of target data-test
[ 66%] Building CXX object CMakeFiles/data- test.dir/data_testsuite.cpp.o
[100%] Linking CXX executable data-test
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/data-test.dir/build.make:86: recipe for target 'data-test' failed
make[2]: *** [data-test] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/data-test.dir/all' failed
make[1]: *** [CMakeFiles/data-test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And I have no idea why is not finding main. Main is part of GTest which I'm including as it can be seen on the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project(data-test C CXX)
set(CMAKE_C_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_C_FLAGS_RELEASE "-g0 -O3 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O3 -Wall")
include(GoogleTest)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(cfg)
include_directories(system)
include_directories(system/include)
include(trace/CMakeLists.txt)
add_executable(data-test
$<TARGET_OBJECTS:trace>
data_testsuite.cpp
)
target_link_libraries(data-test Threads::Threads GTest::GTest)
gtest_discover_tests(data-test)
And as you can see from the make command output, it is compiling just fine.
Do you know what is going on?
The libraries defined by the target GTest::Gtest doesn't contain main function definition. For obtain definition of main you need link with GTest::Main. This is written in documentation.
target_link_libraries(data-test Threads::Threads GTest::Main)
I will try to explain in detail the difficulties I encountered.
Recently I want to use boost libraries in Ubuntu Mint 18 x64. So I download the newest version of it which is 1.62.0. Sha256sum is fine. Then I started compiled it with "./bootstrap.sh" and "./b2". Finally copied then to "/usr/local" with "sudo ./b2 install". So far so good.
I am using Clion right now, so new job is to add libraries in Cmakelist.txt. Code below is what I have added.
set(BOOST_ROOT /usr/local)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.62.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
First thing I want to explain is that I know it is not good to set up a specific location in CMake, but without it Cmake cannot find it. So I just do that. And, at this moment, CMake did not post any error info. Here is my hello_world demo.
#include <iostream>
#include <boost/thread/testable_mutex.hpp>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
I just include a .hpp for test, nothing else. When I attemped to run it. I got these.
-- Boost version: 1.62.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vita-nove/ClionProjects/BoostTest/cmake-build-debug
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/main.cpp.o
[100%] Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/BoostTest.dir/build.make:94: recipe for target 'BoostTest' failed
make[3]: *** [BoostTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/BoostTest.dir/all' failed
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/BoostTest.dir/rule' failed
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
Makefile:118: recipe for target 'BoostTest' failed
make: *** [BoostTest] Error 2
Wired thing is that I do find some .hpp file in External Libraries. They are all located in /include/boost. In /usr/local/lib and /usr/local/include/boost everything shows up as I expect.
Sorry for my bad English. After spending nearly whole day searching and testing I cannot solve it. I don't know what dependencies I have missed or something else. Anyway, Thank you for reading this, hope someone will help.
You have to tell cmake that you need the thread library
set(BOOST_ROOT /usr/local)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.62.0 COMPONENTS thread system) # <-- here
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
I have a project with the following structure
/cmake_modules/
FindSFML.cmake
/includes/
car.hpp
motor.hpp
tires.hpp
/sources/
car.cpp
motor.cpp
tires.cpp
/main.cpp
/main.hpp
I have the following CMakeFiles.txt file:
cmake_minimum_required(VERSION 2.8)
project (MYGAME)
set (MYGAME_VERSION_MAJOR 1)
set (MYGAME_VERSION_MINOR 0)
set (EXECUTABLE_NAME "mygame")
include_directories ("${MYGAME_BINARY_DIR}")
include_directories ("${MYGAME_BINARY_DIR}/includes")
link_directories ("${MYGAME_BINARY_DIR}/sources")
add_executable(${EXECUTABLE_NAME} main.cpp)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.0 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I try to execute make I get this:
[100%] Building CXX object CMakeFiles/mygame.dir/main.cpp.o
Linking CXX executable mygame
CMakeFiles/mygame.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'
main.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'
collect2: error: ld returned 1 exit status
make[2]: *** [mygame] Error 1
make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
make: *** [all] Error 2
How do I fix it?
You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.
You can either add them along with main.cpp in the executable directly:
set(MySources sources/car.cpp sources/motor.cpp sources/tires.cpp main.cpp)
add_executable(${EXECUTABLE_NAME} ${MySources})
or you can make these into a library and link that:
set(MyLibSources sources/car.cpp sources/motor.cpp sources/tires.cpp)
add_library(MyLib ${MyLibSources})
add_executable(${EXECUTABLE_NAME} main.cpp)
...
target_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})
A couple of other points to note:
You should avoid the use of link_directories if possible (its own documentation discourages its use), and it's often helpful to include the headers in the list of files added via add_executable or add_library since these then show up in IDEs like MS Visual Studio.
I am trying to build the hello.c example from http://www.glprogramming.com/red/chapter01.html (look for "Example 1-2").
My CMakeLists.txt is as follows:
cmake_minimum_required (VERSION 2.8)
project (GLUTEX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
include_directories(${OpenGL_INCLUDE_DIRS})
add_executable (glutex glutex.c)
target_link_libraries (glutex ${OpenGL_LIBRARIES})
target_link_libraries (glutex ${GLUT_LIBRARIES})
The CMake call succeeds in generating the required Makefile. But when I call make, I encounter the following:
Scanning dependencies of target glutex
[100%] Building C object CMakeFiles/glutex.dir/glutex.c.o
Linking C executable glutex
/usr/bin/ld: CMakeFiles/glutex.dir/glutex.c.o: undefined reference to symbol 'glOrtho'
/usr/bin/ld: note: 'glOrtho' is defined in DSO /usr/lib64/libGL.so.1 so try adding it to the linker command line
/usr/lib64/libGL.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make[2]: *** [glutex] Error 1
make[1]: *** [CMakeFiles/glutex.dir/all] Error 2
make: *** [all] Error 2
How do I fix this?
Try changing
target_link_libraries (glutex ${OpenGL_LIBRARIES})
to
target_link_libraries (glutex ${OPENGL_LIBRARIES})