Re: [googletest] Shared libraries with gtest and cmake

2,782 views
Skip to first unread message

Vlad Losev

unread,
Jun 12, 2012, 2:52:44 PM6/12/12
to Google C++ Testing Framework
Hi Julius -

Try taking all the OpenCV calls out of your test.cpp. If memory errors go away, the most likely issue is having the OpenCV library compiled with different flags or using a different runtime library.

HTH,
Vlad

On Sun, Jun 10, 2012 at 5:36 PM, jeadorf <jea...@googlemail.com> wrote:
Hi,

I am trying to use gtest in a project, and wish to compile gtest-1.6.0 as a shared library and link it to my tests. I am working on Arch Linux, but it fails for me on Ubuntu 12.04 as well. In a straight-forward attempt I toggled on the BUILD_SHARED_LIBS flag on (also for the gtest-1.6.0 directory). Unfortunately, I run into errors when running the tests, such as "double free or corruption", "segmentation fault", or "munmap_chunk() invalid pointer" or other memory-related violations. I tried to add compilation flags -DGTEST_CREATE_SHARED_LIBRARY=1  and -DGTEST_LINKED_AS_SHARED_LIBRARY=1 as pointed out in the README, but that did not change much.

Here's my CMakeLists.txt

  1 cmake_minimum_required(VERSION 2.6)
  2 project(gtest-issue)
  3 
  4 set(BUILD_SHARED_LIBS on)
  5 
  6 find_package(OpenCV 2.4 REQUIRED)
  7 
  8 enable_testing()
  9 
 10 add_definitions("-DGTEST_CREATE_SHARED_LIBRARY=1")
 11 add_definitions("-DGTEST_LINKED_AS_SHARED_LIBRARY=1")
 12 
 13 add_subdirectory(gtest-1.6.0)
 14 
 15 include_directories(${gtest_SOURCE_DIR})
 16 include_directories(${gtest_SOURCE_DIR}/include)
 17 
 18 add_executable(utest test.cpp)
 19 target_link_libraries(utest ${OpenCV_LIBS} gtest)

And my test.cpp:

  1 #include <gtest/gtest.h>
  2 #include <opencv2/highgui/highgui.hpp>
  3 
  4 class OpenCVTest : public ::testing::Test {
  5 public:
  6 
  7     virtual void SetUp() {
  8     }
  9 
 10 };
 11 
 12 TEST_F(OpenCVTest, ShowImage) {
 13     cv::Mat out = cv::Mat::zeros(300, 300, CV_8UC1);
 14     cv::imshow("out", out);
 15     cv::waitKey(-1);
 16 }
 17 
 18 int main(int argc, char **argv){
 19     ::testing::InitGoogleTest(&argc, argv);
 20     int s = RUN_ALL_TESTS();
 21     return s;
 22 }
 23 

I'm probably doing something wrong. Any help appreciated.

Regards,
Julius




jeadorf

unread,
Jun 19, 2012, 2:02:32 PM6/19/12
to googletes...@googlegroups.com
Hi, 

Try taking all the OpenCV calls out of your test.cpp. If memory errors go away, the most likely issue is having the OpenCV library compiled with different flags or using a different runtime library.

Yes, thanks, without the OpenCV calls, the errors disappear. I will try to link against OpenCV compiled from source and then report back in case I find a solution.

Cheers,
Julius 

brandon c

unread,
Jun 16, 2014, 6:37:38 PM6/16/14
to googletes...@googlegroups.com
-DBUILD_SHARED_LIBS=1  is the flag you need with Cmake for shared libraries the documentation is off
Reply all
Reply to author
Forward
0 new messages