I didn't see where is my problem in my main. I saw a lot of posts and I don't know where my error is. I know it's a problem with the main. I tried to clean and rebuild (qmake -project and qmake) but it still doesn't work.
int main(int ac, char *av[]) {
QApplication app(ac, av);
app.setOrganizationName("Zero");
app.setApplicationName("Gomoku");
IntroState *intro = new IntroState();
if (intro->exec() != QDialog::Accepted)
return 1;
GameEngine engine;
engine.show();
return app.exec();
}
My CMake is:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
project(Gomoku)
set(SOURCE_FILES src/core/IntroState.cpp
src/core/GameEngine.cpp
src/core/Arbiter.cpp
src/tests/CoreFirstRuleTests.cpp)
find_package(Qt5Widgets REQUIRED)
include_directories(include/core
include/ai
include/graphic
include/tests
include)
set(HW_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${HW_HEADER_DIR})
qt5_wrap_cpp(Gomoku_SRC ${HW_HEADER_DIR}/core/GameEngine.h
${HW_HEADER_DIR}/core/IntroState.h)
qt5_wrap_ui(Gomoku_UI
${HW_HEADER_DIR}/ui/dialog.ui)
add_executable(Gomoku ${SOURCE_FILES} ${Gomoku_SRC} ${Gomoku_UI})
qt5_use_modules(Gomoku Widgets)
Don't forget to add your main at the compilation...
Related
I'm trying to build a simple qt project with CMake, which contains main.cxx, MainWindow.cxx and MainWindow.hxx. When I try to make install it says fatal error: 'QMainWindow' file not found, but I do added Widgets in the CMakelists.txt.
Here are the codes:
main.cxx:
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char** argv){
QApplication GUI(argc, argv);
MainWindow window;
window.shou();
return GUI.exec();
}
MainWindow.cxx
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
MainWindow.hxx
#ifndef MAINWINDOW_HXX_
#define MAINWINDOW_HXX_
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent =0);
~MainWindow();
};
#endif
CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(Gui_Window)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt6 COMPONENTS Widgets REQUIRED)
add_library(MAINWINDOW ${CMAKE_CURRENT_SOURCE_DIR}/src/MainWindow.cxx)
add_executable(Gui_Window ${CMAKE_CURRENT_SOURCE_DIR}/app/main.cxx)
target_link_libraries(Gui_Window PUBLIC Qt6::Widgets
MAINWINDOW
)
install(TARGETS Gui_Window DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/bin)
What should be the problem?
Your MainWindow.cxx file should be included in your CMakeLists.txt file like this:
set(PROJECT_SOURCES MainWindow.cxx)
You'd want main in there too.
As an aside, it's much easier to use Qt Creator and let it generate the CMakeLists.txt file for you.
I am trying to compile a group Qt project with cmake. It worked until I added the GUI part.
main.cpp:
#include <QtWidgets>
int start_GUI(int argc, char *argv[]){
QApplication a(argc, argv);
Window * window = new Window();
SSConnection *ssc = new SSConnection();
Window window;
return a;
}
cmake:
cmake_minimum_required(VERSION 3.6)
project(client)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5WebSockets REQUIRED)
find_package(Qt5Gui REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(client_main ${SOURCE_FILES})
qt5_use_modules(client_main Widgets Gui WebSockets)
output:
undefined reference to `Button::mousePressEvent(QGraphicsSceneMouseEvent*)'
undefined reference to `non-virtual thunk to Button::mousePressEvent(QGraphicsSceneMouseEvent*)'
The Button class is just a QObject to add a button to the screen
Button:
class Button : public QObject , public QGraphicsRectItem
{
Q_OBJECT
public:
Button(QString name, QGraphicsItem * parent = NULL);
void mousePressEvent(QGraphicsSceneMouseEvent * event);
signals:
void clicked();
private:
QGraphicsTextItem * text;
QPushButton * button;
QGraphicsRectItem * rect;
};
Any suggestions on how to fix the linking error
To solve the linker error, implement this method or remove it.
void mousePressEvent(QGraphicsSceneMouseEvent * event);
I am having trouble figuring out how to build out a modularized c++ app and getting everything to link up properly. Specifically, I can't get some tests to build because ultimately I get an undefined reference error based on a class method. (I am new to c++ and CMake so I'm probably doing a lot wrong, but I'm learning!) I have gone through other posts and all I can glean is that I'm potentially not linking properly.
Both OrderBook and Strategy build just fine. However, when I go to build StrategyTests and OrderBookTests I get an error:
[ 83%] Linking CXX executable StrategyTests.exe
../../OrderBook/libOrderBook.a(OrderBook.cpp.o): In function `orderbook::OrderBook::notify_strategies(std::shared_ptr<orderbook::Order> const&, bool)':
/cygdrive/d/Dropbox/My Documents/Programming/CLionProjects/TradingSystem/OrderBook/OrderBook.cpp:220: undefined reference to `HYSTRAT::Strategy::onBookUpdate(std::shared_ptr<Events::TOB> const&, std::shared_ptr<Events::OrderBookEvent> const&, bool)'
/cygdrive/d/Dropbox/My Documents/Programming/CLionProjects/TradingSystem/OrderBook/OrderBook.cpp:220:(.text+0x11c4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HYSTRAT::Strategy::onBookUpdate(std::shared_ptr<Events::TOB> const&, std::shared_ptr<Events::OrderBookEvent> const&, bool)'
collect2: error: ld returned 1 exit status
I've tried a number of different things including fiddling with CMakeLists and headers, but none seem to work. It's pretty clear I don't know the true meaning of "undefined reference". I suspect this is an easy fix, but I posted the project setup below in case it's not.
Here's my setup:
Clion and Cygwin on Windows 10
Project Tree
TradingSystem
|-CMakeLists.txt
|\OrderBook
|-|-CMakeLists.txt
|-|-OrderBook.h
|-|-OrderBook.cpp
|\Strategy
|-|-CMakeLists.txt
|-|-Strategy.h
|-|-Strategy.cpp
|\Tests
|-|-CMakeLists.txt
|-|\OrderBookTests
|-|-|-CMakeLists.txt
|-|-|\BoostTests
|-|-|-|-CMakeLists.txt
|-|-|-|-OrderBookBoostTests.cpp
|-|\StrategyTests
|-|-|-CMakeLists.txt
|-|-|-StrategyBoostTests.cpp
CMakes
#TradingSystem/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(TradingSystem)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(TradingSystem ${SOURCE_FILES} ${HEADER_FILES})
include_directories(OrderBook Strategy Events)
add_subdirectory(OrderBook)
add_subdirectory(Strategy)
add_subdirectory(Events)
add_subdirectory(Tests/OrderBookTests)
add_subdirectory(Tests/StrategyTests)
target_link_libraries(TradingSystem OrderBook Strategy)
-
#TradingSystem/OrderBook/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(OrderBook)
include_directories(/cygdrive/c/Program Files/boost/boost_1_66_0)
set(CMAKE_CXX_STANDARD 11)
set(HEADER_FILES OrderBook.h)
set(SOURCE_FILES OrderBook.cpp)
add_library(OrderBook STATIC ${SOURCE_FILES} ${HEADER_FILES})
-
#TradingSystem/Strategy/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Strategy)
include_directories(/cygdrive/c/Program Files/boost/boost_1_66_0)
set(CMAKE_CXX_STANDARD 11)
set(HEADER_FILES Strategy.h)
set(SOURCE_FILES Strategy.cpp)
add_library(Strategy STATIC ${SOURCE_FILES} ${HEADER_FILES})
-
#TradingSystem/Tests/CMakeLists.txt
project(Tests)
add_subdirectory(OrderBookTests)
add_subdirectory(StrategyTests)
-
#TradingSystem/Tests/OrderBookTests/CMakeLists.txt
project(OrderBookTests)
add_subdirectory(BoostTests)
[EDIT]
#TradingSystem/Tests/OrderBookTests/BoostTests/CMakeLists.txt
add_executable(BoostTests OrderBookBoostTests.cpp)
enable_testing()
include_directories(/cygdrive/c/Program Files/boost_1_66_0)
set(BOOST_ROOT "C:/Program Files/boost_1_66_0/")
set(BOOST_LIBRARYDIR "C:/Program Files/boost_1_66_0/")
find_package(Boost 1.66.0)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${BOOSTROOT})
link_directories("${BOOSTROOT}")
target_include_directories(OrderBook PRIVATE ${BOOST_INCLUDE_DIRS})
# Original:
# target_link_libraries(BoostTests OrderBook)
# Changed to:
target_link_libraries(BoostTests Orderbook Strategy)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
[EDIT]
#TradingSystem/Tests/StrategyTests/CMakeLists.txt
project(StrategyTests)
add_executable(StrategyTests StrategyBoostTests.cpp)
enable_testing()
include_directories(/cygdrive/c/Program Files/boost_1_66_0)
set(BOOST_ROOT "C:/Program Files/boost_1_66_0/")
set(BOOST_LIBRARYDIR "C:/Program Files/boost_1_66_0/")
find_package(Boost 1.66.0)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${BOOSTROOT})
link_directories("${BOOSTROOT}")
target_include_directories(Strategy PRIVATE ${BOOST_INCLUDE_DIRS})
# Original:
# target_link_libraries(StrategyTests Strategy OrderBook)
# Change to:
target_link_libraries(StrategyTests Orderbook Strategy)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
The Src Files
#TradingSystem/OrderBook/OrderBook.h
#fndef TRADINGSYSTEM_ORDERBOOK_H
#define TRADINGSYSTEM_ORDERBOOK_H
#include <vector>
using std::vector;
#include "Strategy.h"
#include "Events.h"
namespace orderbook {
/////////////////////
// HIDDEN CODE
/////////////////////
class OrderBook {
private:
vector<HYSTRAT::Strategy> strategies_;
bool notify_strategies(const order_ptr& o, bool add_flag);;
Events::order_book_event_ptr create_order_book_event(const order_ptr& order);;
public:
/////////////////////////////
// CONSTRUTORS; DESTRUCTORS
/////////////////////////////
inline bool subscribe(HYSTRAT::Strategy& s) {
strategies_.push_back(s);
}
#endif //TRADINGSYSTEM_ORDERBOOK_H
-
#TradingSystem/OrderBook/OrderBook.cpp
#include "OrderBook.h"
#include "Events.h"
#include "Strategy.h"
using orderbook::OrderBook;
using HYSTRAT::Strategy;
bool orderbook::OrderBook::notify_strategies(const order_ptr& o, bool add_flag) {
try {
Events::order_book_event_ptr event = create_order_book_event(o);
Events::topOfBook_ptr tob = get_top_of_book();
for (HYSTRAT::Strategy& strategy : strategies_) {
strategy.onBookUpdate(tob, event, add_flag);
}
return true;
} catch (const std::exception& e) {
return false;
}
}
-
#TradingSystem/Strategy/Strategy.h
#ifndef TRADINGSYSTEM_STRATEGY_H
#define TRADINGSYSTEM_STRATEGY_H
#include "Events.h"
namespace HYSTRAT {
class Strategy {
public:
void onBookUpdate(const Events::topOfBook_ptr& tob, const Events::order_book_event_ptr& e, bool event_flag);;
};
}
#endif //TRADINGSYSTEM_STRATEGY_H
-
#TradingSystem/Strategy/Strategy.cpp
#include "OrderBook.h"
#include "Strategy.h"
#include "Events.h"
using namespace HYSTRAT;
void Strategy::onBookUpdate(const Events::topOfBook_ptr &tob, const Events::order_book_event_ptr &e, bool event_flag) {
if (tob->bidP >= tob->offerP) {
quantity_t order_size = std::min(tob->offerQ, tob->bidQ);
order_ptr buy_order(new Order(tob->offerP, order_size, BUY));
order_ptr sell_order(new Order(tob->bidP, order_size, SELL));
send_order(buy_order);
send_order(sell_order);
}
}
I think you are missing a few calls to "add_dependencies" in your CMakeLists.txt files. I think this particular problem might be solved by adding
add_dependencies(OrderBook Strategy)
to your Orderbooks CMakelists.txt
Here is the cmake file that i am using
cmake_minimum_required (VERSION 3.0)
project (midasd)
set (midas VERSION_MAJOR 0)
set (midas VERSION_MINOR 0)
set (midas VERSION_REVISION 1)
find_library(libconfig libconfig)
add_executable(midasd src/main.cpp)
target_link_libraries(midasd "${libconfig_LIBS}")
The problem i am facing is undefined reference for config_init. The main function is as follows
#include <libconfig.h>
int main(int argc, char *argv[])
{
midas::midasCtx *container = new midas::midasCtx(argc,argv);
config_t cfg;
config_init(&cfg);
return 0;
}
Where am i going wrong with CMAKE ?
Actually the libconfig is recognized as simply -lconfig not -llibconfigin linking argument. The CMakeLists.txt should contain
target_link_libraries(my_project config)
Source
This manual(https://hyperrealm.github.io/libconfig/libconfig_manual.html) says " To link with the library, specify ‘-lconfig++’ as an argument to the linker. "
So I fixed like following code and build was completed.
target_link_libraries(my_project config++)
I am a newcomer to the world of cmake. This question came up while I was experimenting with some basic cmake configurations in C++. To be precise, following is my directory structure :
/src----
|-> CMakeLists.txt
|-> main.cpp
|-> lib----
|-> libfsystem.so
|->filesystem----
|->CMakeLists.txt
|->listfiles.cpp
|->include-----
|->fsystem.h
Now, the /src/filesystem/CMakeLists.txt file is like this
cmake_minimum_required(VERSION 2.8)
project(fsystem)
set(CMAKE_BUILD_TYPE Release)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/build)
find_package(Boost REQUIRED system filesystem)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(fsystem SHARED listfiles.cpp)
While, the /src/CMakeLists.txt file is like this
cmake_minimum_required(VERSION 2.8)
project(vessel_detect)
add_subdirectory(filesystem)
add_executable(main main.cpp)
set(CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
find_library(libpath fsystem)
MESSAGE(${libpath})
target_link_libraries(main ${libpath})
The library libfsystem.so is successfully created.
The library libfsystem.so is also successfully found by /src/CMakeLists.txt
However when the linking of main.cpp is done, then it gives me several undefined reference errors which should not have happened as everything has already been defined. For greater completeness, following is the content of main.cpp file
Main.cpp
#include<iostream>
#include"filesystem/include/fsystem.h"
using namespace std;
int main(void)
{
string path ("~");
vector<string>* output;
output = listfiles(path);
return 0;
}
The contents of listfiles.cpp are
listfiles.cpp
#include"fsystem.h"
using namespace boost::filesystem;
vector<string>* listfiles(int argc, char* argv[])
{
if (argc<2)
{
std::cout<<"No file name provided"<<std::endl;
}
else
{
path p(argv[1]);
vector<string>* output;
if (exists(p))
{
if(is_directory(p))
{
std::cout<<"You specified a directory"<<std::endl;
std::cout<<"Its contents are as follows :-"<<std::endl;
typedef std::vector<path> vec;
vec v;
copy(directory_iterator(p),directory_iterator(),back_inserter(v));
sort(v.begin(),v.end());
for(vec::const_iterator it(v.begin());it!=v.end();++it)
output->push_back(it->filename().string());
// std::cout<<it->filename()<<std::endl;
}
else if (is_regular_file(p))
{
std::cout<<argv[1]<<" "<<file_size(p)<<std::endl;
}
else
{
std::cout<<"The file is neither a directory nor a regular file"<<std::endl;
}
}
else
{
std::cout<<"The speicified path does not exist"<<std::endl;
}
}
}
And finally, the fsystem.h contents are :
fsystem.h
#ifndef _fsystem_h
#define _fsystem_h
#include<iostream>
#include<string>
#include<vector>
#include"boost/filesystem.hpp"
using namespace std;
vector<string>* listfiles(string);
#endif
Could someone provide me a reason for the undefined reference errors I am getting during the linking of main.cpp ? I would also be grateful if you could provide me with a resolution of this issue.
Thanks
Your not linking boost. you need to add target_link_libraries(fsystem ${Boost_LIBRARIES}) to the end of /src/filesystem/CMakeLists.txt and include_directories(${Boost_INCLUDE_DIRS}) between the find_package and add_library.
cmake_minimum_required(VERSION 2.8)
project(fsystem)
set(CMAKE_BUILD_TYPE Release)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/build)
find_package(Boost REQUIRED system filesystem)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${Boost_INCLUDE_DIRS})
add_library(fsystem SHARED listfiles.cpp)
target_link_libraries(fsystem ${Boost_LIBRARIES})
(1) For TARGET_LINK_LIBRARIES you should put the name of the target, thus:
TARGET_LINK_LIBRARIES(main fsystem)
(2) You declare listfiles as vector<string>* listfiles(string) while you define it as vector<string>* listfiles(int,char**)
Additionally you need to link with Boost per the other reply.