Ubuntu16.04+CLion编译OpenCV2.4.9程序报undefined reference错误求助
Hey there, let's tackle that undefined reference error you're facing—this is almost always a linker problem, meaning your CLion project isn't properly connecting to the OpenCV 2.4.9 libraries during the build process. Let's fix this step by step:
1. Fix Your CMakeLists.txt (Critical for CLion)
CLion uses CMake to manage builds, so your CMakeLists.txt needs to explicitly tell the linker where to find OpenCV's libraries. Replace your current CMake file with this template, adjusting the project/executable names to match yours:
cmake_minimum_required(VERSION 3.5) project(your_opencv_demo) # Replace with your actual project name set(CMAKE_CXX_STANDARD 11) # Ubuntu 16.04 supports this standard by default # Locate OpenCV 2.4.9 on your system find_package(OpenCV 2.4.9 REQUIRED) # Include OpenCV's header directories so your code can compile include_directories(${OpenCV_INCLUDE_DIRS}) # Add your main source file to the build add_executable(your_demo_executable main.cpp) # Replace with your executable name # Link all required OpenCV libraries to your program (this fixes the undefined reference errors) target_link_libraries(your_demo_executable ${OpenCV_LIBS})
2. Verify OpenCV 2.4.9 is Properly Installed
Before blaming CLion, confirm OpenCV is correctly set up on your Ubuntu system:
- Open a terminal and run
pkg-config --modversion opencv—you should see2.4.9as the output. If not, you'll need to reinstall OpenCV 2.4.9 (either via apt or from source). - Check that the libraries exist: Navigate to
/usr/local/lib(if you installed from source) or/usr/lib/x86_64-linux-gnu(if you used apt) and look for files starting withlibopencv_(e.g.,libopencv_core.so,libopencv_highgui.so).
3. Clean Up CLion's CMake Cache
Sometimes CLion caches old build configurations, which can cause issues even after fixing your CMakeLists.txt:
- Go to
File > Clear CMake Cache - Then click the "Reload CMake Project" button (the circular arrow next to the build target selector in the top-right corner)
4. Optional: Switch to OpenCV's C++ API (More Robust)
Your current code uses the old C-style OpenCV API (IplImage, cvLoadImage), which is deprecated. Switching to the C++ API will make your code safer and easier to maintain, plus it's less likely to run into subtle linking issues. Here's an updated version of your code:
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> int main(int argc, char **argv) { // Check if an image path was provided as an argument if (argc != 2) { std::cout << "Usage: ./your_demo_executable path/to/image.jpg" << std::endl; return 1; } // Load the image using the modern C++ API cv::Mat img = cv::imread(argv[1]); if (img.empty()) { std::cout << "Error: Could not read the image!" << std::endl; return 1; } // Display the image cv::namedWindow("Loaded Image", cv::WINDOW_NORMAL); cv::imshow("Loaded Image", img); cv::waitKey(0); // Wait for user to press a key before closing return 0; }
After following these steps, your build should succeed. If you still run into issues, double-check that your CMakeLists.txt has the correct project/executable names, and that OpenCV 2.4.9 is indeed installed on your system.
内容的提问来源于stack exchange,提问作者Qohelet




