I am learning to use cmake and I am trying to compile a simple set of tests using gtest for a very small project that I wrote.
my CMakeLists.txt looks like
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)
I got most of it from the googletest docs. I am trying to compile an executable that has a main() in test.cpp and relies on a Connector class in Connector.cpp and Connector.h. All files are in the same directory
when I run cmake . and then make I get the following error:
/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
It seems that cmake did not know to include Connector.cpp in the compilation even though I specified it in the CMakeLists.txt. What am I doing wrong?
Code for Connector.cpp, currently it doesn't do very much lol but it's a 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;
}
Code for 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());
}
Since you need external class, you should put that class "library" in target_link_libraries(), so that the last line of your CMakeLists.txt will become
target_link_libraries(test connector gtest_main)
Also, in another CMakeLists.txt which is in the same directory as Connector.cpp, your should declare them as one library:
set(SOURCES Connector.cpp)
add_library(connector STATIC ${SOURCES})
Related
I am trying to create a simple OpenSSL poject in CLion but it can suceed in linking it.
This is my final CMakeLists.txt file (after many tries) which gives less errors:
cmake_minimum_required(VERSION 3.16)
project(duplicates_finder)
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L . -lssl -lcrypto")
set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(OpenSSL REQUIRED)
if (OPENSSL_FOUND)
# Add the include directories for compiling
target_include_directories(${PROJECT_NAME} PUBLIC ${OPENSSL_INCLUDE_DIR})
# Add the static lib for linking
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)
message(STATUS "Found OpenSSL ${OPENSSL_VERSION}")
else()
message(STATUS "OpenSSL Not Found")
endif()
include_directories(C:\\OpenSSL-Win32\\include)
link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
This is the file I am trying to compile:
#include <string>
#include <iostream>
#include <filesystem>
#include <string>
#include <openssl/sha.h>
#include "openssl/ssl.h"
#include <sstream>
#include <iomanip>
using namespace std;
namespace fs = std::filesystem;
//string sha256(const string& str)
//{
// unsigned char hash[SHA256_DIGEST_LENGTH];
// SHA256_CTX sha256;
// SHA256_Init(&sha256);
// SHA256_Update(&sha256, str.c_str(), str.size());
// SHA256_Final(hash, &sha256);
// stringstream ss;
// for(unsigned char i : hash)
// {
// ss << hex << setw(2) << setfill('0') << (int)i;
// }
// return ss.str();
//}
int main() {
std::cout << "SSLeay Version: " << SSLeay_version(SSLEAY_VERSION) << std::endl;
SSL_library_init();
auto ctx = SSL_CTX_new(SSLv23_client_method());
if (ctx) {
auto ssl = SSL_new(ctx);
if (ssl) {
std::cout << "SSL Version: " << SSL_get_version(ssl) << std::endl;
SSL_free(ssl);
} else {
std::cout << "SSL_new failed..." << std::endl;
}
SSL_CTX_free(ctx);
} else {
std::cout << "SSL_CTX_new failed..." << std::endl;
}
}
And these are the errors:
====================[ Build | all | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2020.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\USERNAME\CLionProjects\duplicates_finder\cmake-build-debug --target all -- -j 8
[ 50%] Linking CXX executable duplicates_finder.exe
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\duplicates_finder.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:42: undefined reference to `SSLeay_version'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:43: undefined reference to `SSL_library_init'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:44: undefined reference to `SSLv23_client_method'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:44: undefined reference to `SSL_CTX_new'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:46: undefined reference to `SSL_new'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:48: undefined reference to `SSL_get_version'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:49: undefined reference to `SSL_free'
C:/PROGRA~2/EMBARC~1/Dev-Cpp/TDM-GC~1/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/USERNAME/CLionProjects/duplicates_finder/main.cpp:53: undefined reference to `SSL_CTX_free'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [duplicates_finder.exe] Error 1
CMakeFiles\duplicates_finder.dir\build.make:87: recipe for target 'duplicates_finder.exe' failed
mingw32-make.exe[1]: *** [CMakeFiles/duplicates_finder.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/duplicates_finder.dir/all' failed
Makefile:82: recipe for target 'all' failed
I restared my PC.
I also added the line:
target_link_libraries(${PROJECT_NAME} libeay32.lib) //also .a file the result is the same.
And it throws the error that it can't find that file.
Also added the files: libeay32.a, libeay32.def, libeay32.lib and ssleay32.a, ssleay32.def, ssleay32.lib to the project folder and also to the cmak-build-debug folder. => Still not working.
Also renamed libeay32.a to libeay32.dll.a and ssleay32.a to ssleay32.dll.a as on the another topic from stackoverflow says but it is still not working too.
No matter I do it is not compiling at all.
I spent all day searching for a solution but in vain.
I am using Windows 7 x64 and OpenSSL 1.1.1m 14 Dec 2021.
Thank you in advance!
I can't seem to link SDL2 correctly. I am using CLion with MinGW. My CMakeLists.txt looks like this currently, but I tried using the FindSDL2 module floating around, though it always said that it could not find the SDL2 library. This is the only way I've found it to stop giving back errors.
cmake_minimum_required(VERSION 3.16)
project(CBeat)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "CC:/Development/SDL2_MinGW_32bit/lib/cmake/SDL2")
set(SDL2_DIR C:/Mingw)
set(CMAKE_CXX_STANDARD 14)
set(SDL2_LIB_DIR ${SDL2_DIR}/lib)
include_directories(${SDL2_DIR}/include)
add_executable(CBeat main.cpp)
target_link_libraries(CBeat ${SDL2_LIB_DIR}/libSDL2.dll.a ${SDL2_LIB_DIR}/libSDL2main.a ${SDL2_LIB_DIR}/libSDL2.a -mwindows -lmingw32 -lSDL2main -lSDL2)
add_definitions(-DSDL_MAIN_HANDLED)
Now, the main.cpp looks like this, CLion shows no errors and actually is able to give me suggestions and information in each function, but when running it marks various functions as undefined. I attach evidence. I am so desperate right now, I've been trying to figure out this for days now and nothing seems to work!
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
const int SWIDTH=640, SHEIGHT=420;
int main( int argc, char* args[] ){
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
cout<< "SDL could not initialize! SDL_Error:" << SDL_GetError << endl;
}
else
{
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SWIDTH, SHEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
cout << "Window could not be created! SDL_Error: " << SDL_GetError << endl;
}
else
{
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL,SDL_MapRGB(screenSurface->format, 0xFF,0xFF,0xFF));
SDL_UpdateWindowSurface( window );
SDL_Delay( 2000 );
}
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
}
CLion trying to be useful
And these are the errors it gives back:
[ 50%] Linking CXX executable CBeat.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\CBeat.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/yello/CLionProjects/CBeat/main.cpp:12: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:18: undefined reference to `SDL_CreateWindow'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:25: undefined reference to `SDL_GetWindowSurface'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:26: undefined reference to `SDL_MapRGB'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:26: undefined reference to `SDL_FillRect'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:27: undefined reference to `SDL_UpdateWindowSurface'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:28: undefined reference to `SDL_Delay'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:30: undefined reference to `SDL_DestroyWindow'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:31: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\CBeat.dir\build.make:88: recipe for target 'CBeat.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/CBeat.dir/all' failed
mingw32-make.exe[3]: *** [CBeat.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/CBeat.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/CBeat.dir/rule] Error 2
mingw32-make.exe: *** [CBeat] Error 2
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/CBeat.dir/rule' failed
Makefile:117: recipe for target 'CBeat' failed
So I have OpenCV libraries downloaded from here
https://sourceforge.net/projects/opencvlibrary/files/3.4.6/opencv-3.4.6-vc14_vc15.exe/download
CMake gui with cmakelist.txt like this
cmake_minimum_required(VERSION 2.8)
project(Display)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library
status:") message(STATUS " config:
${OpenCV_DIR}") message(STATUS "
version: ${OpenCV_VERSION}")
message(STATUS " libraries:
${OpenCV_LIBS}") message(STATUS "
include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(Display
DisplayImage.cpp)
target_link_libraries(Display
${OpenCV_LIBS})
and have a simple cpp like this
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv )
{
// cout << "You have entered " << argc
// << " arguments:" << "\n";
// for (int i = 0; i < argc; ++i)
// cout << argv[i] << "\n";
// cout << "a" << argv[1];
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
printf("Hello World!");
return 0;
}
already configure it with cmake-gui and this shows up
OpenCV library status:
config: D:/opencv/build/x64/vc15/lib
version: 3.4.6
libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world
include path: D:/opencv/build/include;D:/opencv/build/include/opencv;D:/opencv/build/include/opencv2
Configuring done Generating done
seems good right ? and then on the build directory from command prompt, i typed mingw32-make
and this shows up
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x72):
undefined reference to
cv::imread(cv::String const&, int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0xe3):
undefined reference to
cv::namedWindow(cv::String const&,
int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x129):
undefined reference to
cv::imshow(cv::String const&,
cv::_InputArray const&)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x149):
undefined reference to
cv::waitKey(int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42):
undefined reference to
cv::String::allocate(unsigned int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$ZN2cv6StringaSERKS0[__ZN2cv6StringaSERKS0_]+0x1c):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to
cv::fastFree(void*)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40):
undefined reference to
cv::Mat::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4):
undefined reference to
cv::fastFree(void*)' collect2.exe:
error: ld returned 1 exit status
mingw32-make[2]: *
[CMakeFiles\Display.dir\build.make:104:
Display.exe] Error 1 mingw32-make[1]:
* [CMakeFiles\Makefile2:72: CMakeFiles/Display.dir/all] Error 2
mingw32-make: *** [Makefile:83: all]
Error 2
how do i fix this ? or is it because that i use the library straight from the website ?
i also try something like putting a wrong parameter on the imread function and then type mingw32-make and it did tell me that i use a wrong parameter. If they know i used the wrong parameter, then why they can't tell what is imread for (or why is it undefined reference ? ) ? I'm using windows.
I'm very new to these libraries, mingw, and cmake, so i'm sorry if my question is stupid.
this question is not duplicate because, i use windows and mingw32-make, everyone that asks this question did not use that and the question is in a different situation than mine.
So I am trying to get SDL2 to work with CLion (So that I can experiment/learn).
My main code is as such:
#include <iostream>
#include <SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool init();
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool success = true;
/*if(SDL_Init(SDL_INIT_VIDEO)<0){
success = false;
}
else{
}*/
return success;
}
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
And my CMake file looks like this
cmake_minimum_required(VERSION 3.6)
project(SDL2_Lesson_1)
set(CMAKE_CXX_STANDARD 11)
# includes cmake/FindSDL2.cmake
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
set(SOURCE_FILES Lesson_1.cpp)
add_executable(SDL2_App ${SOURCE_FILES})
target_link_libraries(SDL2_App ${SDL2_LIBRARY})
set(SOURCE_FILES Lesson_1.cpp)
add_executable(SDL2_Lesson_1 ${SOURCE_FILES})
Also, I have a file FindSDL2.cmake from here in a folder cmake within the project folder.
Now, with the files as I have them posted, everything compiles and runs fine. BUT, when I uncomment the commented section within init(), the compilation breaks down and gives me the following error:
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
init() in Lesson_1.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [SDL2_Lesson_1] Error 1
make[2]: *** [CMakeFiles/SDL2_Lesson_1.dir/all] Error 2
make[1]: *** [CMakeFiles/SDL2_Lesson_1.dir/rule] Error 2
make: *** [SDL2_Lesson_1] Error 2
Note: Lesson_1.cpp is the file with the main code. Also, this is only part of the error.
Use find_library() instead of find_package()
find_library(SDL2_LIBRARY SDL2 "path/to/your/library_bundle")
find_library(SDL2_App ${SDL2_LIBRARY})
I'm using Ubuntu 12 64 bit, installed fftw library version 2.1.5.
I have a c++ project with use CMake to build the make file. This is my cmakelist.text:
project(MP)
cmake_minimum_required(VERSION 2.8)
if(TYPE STREQUAL "Debug")
set(CMAKE_BUILD_TYPE "Debug")
else()
set(CMAKE_BUILD_TYPE "Release")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
add_definitions( -std=c++11 )
endif()
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_library(GLUI libglui.a ./vendor/lib)
include_directories(${OPENGL_INCLUDE_DIR}
./vendor/include)
LINK_DIRECTORIES(/usr/local/lib)
LINK_DIRECTORIES(/usr/lib)
LINK_DIRECTORIES(/usr/bin)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI})
When I tried running the make file create by Cmake, i got this problem:
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::init_simulation(unsigned long)':
Simulation.cc:(.text+0x2d5): undefined reference to `rfftw2d_create_plan'
Simulation.cc:(.text+0x2ee): undefined reference to `rfftw2d_create_plan'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::solve()':
Simulation.cc:(.text+0x881): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0x891): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0xa7f): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0xa8f): undefined reference to `rfftwnd_one_complex_to_real'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::FFT(int, void*)':
Simulation.cc:(.text+0x390): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0x3a0): undefined reference to `rfftwnd_one_real_to_complex'
collect2: error: ld returned 1 exit status
make[2]: *** [SciVis] Error 1
make[1]: *** [CMakeFiles/SciVis.dir/all] Error 2
make: *** [all] Error 2
In my Simulation.cc file:
#include <fftw.h>
void Simulation::init_simulation(size_t n)
{
//Allocate data structures
size_t dim = n * 2 * (n / 2 + 1);
vx = new fftw_real[dim];
vy = new fftw_real[dim];
vx0 = new fftw_real[dim];
vy0 = new fftw_real[dim];
fx = new fftw_real[n * n];
fy = new fftw_real[n * n];
rho = new fftw_real[n * n];
rho0 = new fftw_real[n * n];
plan_rc = rfftw2d_create_plan(n, n, FFTW_REAL_TO_COMPLEX, FFTW_IN_PLACE);
plan_cr = rfftw2d_create_plan(n, n, FFTW_COMPLEX_TO_REAL, FFTW_IN_PLACE);
// Initialize data structures to 0
for (size_t i = 0; i < n * n; i++)
{
vx[i] = vy[i] = vx0[i] = vy0[i] = fx[i] = fy[i] = rho[i] = rho0[i] = 0.0f;
}
}
void Simulation::FFT(int direction,void* vx)
{
if(direction==1) rfftwnd_one_real_to_complex(plan_rc,(fftw_real*)vx,(fftw_complex*)vx);
else rfftwnd_one_complex_to_real(plan_cr,(fftw_complex*)vx,(fftw_real*)vx);
}
I dont know where I were wrong, can someone please help me ?
Thank you very much.
You're not linking against FFTW, you need to make CMake find the library and link against it first, put this file in your project's directory under a new folder "CMakeModules".
FindFFTW.cmake
# - Find FFTW
# Find the native FFTW includes and library
#
# FFTW_INCLUDES - where to find fftw3.h
# FFTW_LIBRARIES - List of libraries when using FFTW.
# FFTW_FOUND - True if FFTW found.
if (FFTW_INCLUDES)
# Already in cache, be silent
set (FFTW_FIND_QUIETLY TRUE)
endif (FFTW_INCLUDES)
find_path (FFTW_INCLUDES fftw3.h)
find_library (FFTW_LIBRARIES NAMES fftw3)
# handle the QUIETLY and REQUIRED arguments and set FFTW_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES)
mark_as_advanced (FFTW_LIBRARIES FFTW_INCLUDES)
Next, add this line to the top of your CMakeLists.txt:
project(MP)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})
Now try this:
find_package(FFTW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${FFTW_INCLUDES}
./vendor/include)
...
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI} ${FFTW_LIBRARIES})