I get a linker error if I put C_OBJECT into my header.
Currently I copy my QT dll library to my project, otherwise, I get exit code error as I don't know how to add QT library in cMake yet... Not sure if that is causing the error. Maybe I need to add Q_OBJECT in CMakeList.txt somehow?
Here is the sample code:
main.cpp
#include <iostream>
#include <QtWidgets/QApplication>
#include "testWidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
testWidget w;
w.show();
return a.exec();
}
testWidget.h
#ifndef QOBJECTCRASH_TESTWIDGET_H
#define QOBJECTCRASH_TESTWIDGET_H
#include <QtWidgets/QMainWindow>
class testWidget : public QMainWindow
{Q_OBJECT //this crashes the app. Remove it and all runs.
public:
testWidget(QWidget *parent = Q_NULLPTR);
};
#endif //QOBJECTCRASH_TESTWIDGET_H
testWidget.cpp
#include "testWidget.h"
testWidget::testWidget(QWidget *parent): QMainWindow(parent) {
setWindowTitle("Test Q_OBJECT");
setGeometry(500,500,500,500);
}
CMakeList.txt
cmake_minimum_required(VERSION 3.8)
project(QObjectCrash)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "C:\\Qt\\5.9\\mingw53_32")
set(SOURCE_FILES main.cpp testWidget.cpp testWidget.h)
add_executable(QObjectCrash ${SOURCE_FILES})
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
qt5_use_modules(QObjectCrash Core Widgets Gui)
target_link_libraries(QObjectCrash Qt5::Widgets Qt5::Core Qt5::Gui)
Error print from cLion:
CMakeFiles\QObjectCrash.dir/objects.a(main.cpp.obj): In function `ZN10testWidgetD1Ev':
D:/QObjectCrash/testWidget.h:11: undefined reference to `vtable for testWidget'
D:/QObjectCrash/testWidget.h:11: undefined reference to `vtable for testWidget'
CMakeFiles\QObjectCrash.dir/objects.a(testWidget.cpp.obj): In function `ZN10testWidgetC2EP7QWidget':
D:/QObjectCrash/testWidget.cpp:7: undefined reference to `vtable for testWidget'
D:/QObjectCrash/testWidget.cpp:7: undefined reference to `vtable for testWidget'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\QObjectCrash.dir\build.make:126: recipe for target 'QObjectCrash.exe' failed
mingw32-make.exe[3]: *** [QObjectCrash.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/QObjectCrash.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/QObjectCrash.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/QObjectCrash.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/QObjectCrash.dir/rule] Error 2
mingw32-make.exe: *** [QObjectCrash] Error 2
Makefile:117: recipe for target 'QObjectCrash' failed
Thanks for help!
Related
Here is the complete error:
Undefined symbols for architecture arm64:
"ug::UgWindow::UgWindow(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
"ug::UgWindow::~UgWindow()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled-game] Error 1
make[1]: *** [CMakeFiles/untitled-game.dir/all] Error 2
make: *** [all] Error 2
I am using cmake to build my files, i have created a UgWindow hpp file and cpp file to define its functionality here is the hpp file and cpp file.
HPP
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <string>
namespace ug {
class UgWindow{
public:
UgWindow(int w, int h, std::string name);
~UgWindow();
bool shouldClose(){ return glfwWindowShouldClose(window); }
private:
void initWindow();
const int width;
const int height;
std::string windowName;
GLFWwindow *window;
};
}
here is the cpp file:
#include "ug_window.hpp"
namespace ug {
UgWindow::UgWindow(int w, int h, std::string name): width{w}, height{h}, windowName{name} {
initWindow();
}
UgWindow::~UgWindow() {
glfwDestroyWindow(window);
glfwTerminate();
}
void UgWindow::initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
}
}
I am trying to use vulkan with glfw and glm, im a noob with c++ and have absolutely no idea how to fix this or what the problem is.
Also this is my cmake file:
project(untitled-game)
cmake_minimum_required(VERSION 3.22.4)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++17 " )
add_executable(${PROJECT_NAME} main.cpp)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)
if (VULKAN_FOUND)
message(STATUS "Found Vulkan, Including and Linking now")
include_directories(${Vulkan_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)
add_executable(${PROJECT_NAME} main.cpp)
Here you say that the executable program is to be built from the main.cpp source file, and only the main.cpp source file.
You need to list all your source files:
add_executable(${PROJECT_NAME} main.cpp ug_window.cpp)
This is the root of my project. I think I am missing a basic concept because the error occur when I wrap the the find() function in a struct.
CMakeLists.txt
bst.cpp
bst.hpp
bst-test.cpp
catch.hpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.4 FATAL_ERROR)
project(bst LANGUAGES CXX)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(bst bst.cpp)
add_executable(bst-test bst-test.cpp)
target_link_libraries(bst-test bst)
enable_testing()
add_test(
NAME catch_test
COMMAND $<TARGET_FILE:bst-test> --success
)
bst.cpp
struct Bst {
int find(int num, int array[]) { return -1; }
};
bst.hpp
struct Bst {
int find(int num, int array[]);
};
bst-test.cpp
#include "bst.hpp"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("The number to search is not found in the list", "[notFound]") {
int array[]{};
Bst tree;
REQUIRE(tree.find(1, array) == -1);
}
This is the error when trying to compile.
CMakeFiles/bst-test.dir/bst-test.cpp.o: In function `____C_A_T_C_H____T_E_S_T____0()':
bst-test.cpp:(.text+0x2b0b5): undefined reference to `Bst::find(int, int*)'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/bst-test.dir/build.make:84: recipe for target 'bst-test' failed
make[2]: *** [bst-test] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/bst-test.dir/all' failed
make[1]: *** [CMakeFiles/bst-test.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2
you are declaring the Bst structure again in your .cpp file. The source file should only contain the definition of the methods, not the declaration of the structure.
Changing bst.cpp to the following fixes the error:
#include "bst.hpp"
int Bst::find(int num, int array[]) { return -1; }
I have a class threadpool. I have a header file threadpool.h and the implementation threadpool.cc. When trying to I initialize the threadpool like so: ThreadPool *tp = new ThreadPool(); I get the following linker error:
/usr/bin/ld: ../src/libmapreducelib.a(mapreduce_impl.cc.o): in function `Master::Master(MapReduceSpec const&, std::vector<FileShard, std::allocator<FileShard> > const&)':
/aos-pr4/src/master.h:43: undefined reference to `ThreadPool::ThreadPool()'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/mrdemo.dir/build.make:127: bin/mrdemo] Error 1
make[1]: *** [CMakeFiles/Makefile2:330: test/CMakeFiles/mrdemo.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
Here is my project source hierarchy:
src/
>CMakeLists.txt
>threadpool.h
>threadpool.cc
>otherfiles
CMakeLists.txt
Below is my threadpool.h, threadpool.cc and CMakeLists.txt file.
// in theadpool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
#pragma once
class ThreadPool {
public:
using Task = std::function<void()>;
// constructor, init static number of workers to be added to queue
ThreadPool()
ThreadPool(int numOfWorkers);
virtual ~ThreadPool();
....
}
#endif
// in threadpool.cc
#include "threadpool.h"
ThreadPool::ThreadPool(){
// commented out
}
....
I even ensured my cmakelist library includes the threadpool.h.
//CMakeLists.txt
project(mapreducer)
include(GenerateProtos.cmake)
add_library(
mapreducelib #library name
mapreduce.cc mapreduce_impl.cc #sources
master.h mapreduce_spec.h file_shard.h threadpool.h) #headers
target_link_libraries(mapreducelib p4protolib)
target_include_directories(mapreducelib PUBLIC ${MAPREDUCE_INCLUDE_DIR})
add_dependencies(mapreducelib p4protolib)
I believe it should be fine and I've used my threadpool in another project. It must be the project hierarchy.
I am trying to compile simple gmock example on my ubuntu vmware(16.04 LTS)
and getting below error while doing "make"
I have below files -
"test.h"
class CBasicMath
{
public:
CBasicMath(){}
virtual ~CBasicMath() {}
virtual int Addition(int x, int y);
virtual int Multiply(int x, int y);
virtual int Divide(int x, int y);
};
"test.cpp"
#include "test.h"
int CBasicMath::Addition(int x, int y)
{
return (x + y);
}
int CBasicMath::Multiply(int x, int y)
{
return (x * y);
}
int CBasicMath::Divide(int x, int y)
{
return (x / y);
}
"mocktest.h"
#include "gmock/gmock.h"
#include "test.cpp"
class MockBasicTest : public CBasicMath {
public:
MOCK_METHOD2(Addition, int(int x, int y));
MOCK_METHOD2(Multiply, int(int x, int y));
MOCK_METHOD2(Divide, int(int x, int y));
};
"main.cpp"
#include "mocktest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
TEST(BasicMathTest, testAddition) {
MockBasicTest basictest;
EXPECT_CALL(basictest, Addition(2,3)).Times(0);
// EXPECT_EQ(0, basictest.Addition(2,3));
/*
.Times(5);
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
EXPECT_EQ(0,basictest.Addition(2,3));
*/
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
"CMakeLists.txt"
cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests main.cpp)
target_link_libraries(runTests -lgtest -lgmock -lpthread)
These are the steps I followed for compilation -
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ cmake CMakeLists.txt
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ajay/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$
and after that When I did make I am facing the issue
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ make
Scanning dependencies of target runTests
make[2]: Warning: File 'main.cpp' has modification time 84978 s in the future
[ 50%] Building CXX object CMakeFiles/runTests.dir/main.cpp.o
[100%] Linking CXX executable runTests
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
CMakeFiles/runTests.dir/build.make:94: recipe for target 'runTests' failed
make[2]: *** [runTests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
ajay#ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$
I don't know why this "/usr/bin/ld: cannot find -lgmock" issue is coming even though I have installed gmock successfully.
I am able to run gtest programs but when I am adding gmock I am getting this issue.
Please help me to resolve.
Let me know for more info.
Lookup the documentation for taget_link_libraries. Check the comments in FindGtest.cmake
You should not specify libraries them with -l instead use the variables from find_package e.g. ${GTEST_LIBRARIES}
You haven't done find_package for GMOCK so there are no variables defined for GMOCK. As this is not a standard CMake module, write your own or take one from the Internet
BUT, the Google test documentation recommends not use the installed libraries from the system, but to build them yourself inside your Project.There are several examples on the internet how to add gtest/gmock as ExternalProject to your cmake project.
i tryng to build a small example of Xerces with xerces c++ 3.1 and cmake, but i and only getting linkings problems.
This is my cmkelists.txt:
//============================================================================
project(ConfiguradorXerces)
cmake_minimum_required(VERSION 2.8)
include_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/.libs)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/)
set ( XercesLib xerces-c )
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${XercesLib})
//==============================================
//===============================================
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
using namespace xercesc;
using namespace std;
int main()
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
//==============================================
and this is my console output:
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x25): undefined reference to `xercesc_3_1::XMLUni::fgXercescDefaultLocale'
main.cpp:(.text+0x2a): undefined reference to `xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*)'
main.cpp:(.text+0x2f): undefined reference to `xercesc_3_1::XMLPlatformUtils::Terminate()'
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_3_1::XMLException'
collect2: error: ld returned 1 exit status
make[2]: *** [ConfiguradorXerces] Error 1
make[1]: *** [CMakeFiles/ConfiguradorXerces.dir/all] Error 2
make: *** [all] Error 2
16:28:55: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ConfiguradorXerces (target: Desktop)
When executing step 'Make'
//
i was thinking trhat my cmakeLsits.txt was not complete, there is an especial setup that it has to be done??
thx in advance
I am pretty sure that target_link_libraries() macro accepts a target as its first parameter:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
And you have forgot to specify it. So instead of target_link_libraries(${XercesLib}), try target_link_libraries(${PROJECT_NAME} ${XercesLib}).
Hopefully, that solves it.