Cocos2D-X compile Linux Error: Undefined Reference - c++

I'm new to cocos2D-X (v 3.6), I'm using Linux to compile and run my project. When I try the following command line:
cocos compile -p linux
It gives me errors:
Linking CXX executable bin/MyGame
CMakeFiles/MyGame.dir/Classes/AppDelegate.cpp.o: In function `AppDelegate::applicationDidFinishLaunching()':
/home/caisar/MyCompany/MyGame/Classes/AppDelegate.cpp:53: undefined reference to `GraphicsScene::createScene()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/MyGame] Error 1
make[1]: *** [CMakeFiles/MyGame.dir/all] Error 2
make: *** [all] Error 2
Error running command, return code: 2
This is my GraphicsScene.h:
#ifndef __GRAPHICS_SCENE_H__
#define __GRAPHICS_SCENE_H__
#include "cocos2d.h"
class GraphicsScene : public cocos2d::Layer{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(GraphicsScene);
};
#endif
And this is my GraphicsScene.cpp:
#include "GraphicsScene.h"
USING_NS_CC;
Scene* GraphicsScene::createScene(){
auto scene = Scene::create();
auto layer = GraphicsScene::create();
scene->addChild(layer);
return scene;
}
bool GraphicsScene::init(){
if(!Layer::init()){
return false;
}
auto sprite = Sprite::create("autobot.png");
sprite->setPosition(0,0);
this->addChild(sprite, 0);
return true;
}
void GraphicsScene::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
Is there anything else I need to add?

I also had this problem today and solved it by editing the PROJECT_PATH/CMakeLists.txt file. You must include the *.cpp and *.h filepaths in the GAME_SRC and GAME_HEADERS sections. For example, in your case it must be something like this:
set(GAME_SRC
Classes/AppDelegate.cpp
Classes/HelloWorld.cpp
Classes/GraphicsScene.cpp
${PLATFORM_SPECIFIC_SRC}
)
set(GAME_HEADERS
Classes/AppDelegate.h
Classes/HelloWorld.h
Classes/GraphicsScene.h
${PLATFORM_SPECIFIC_HEADERS}
)
Hope that it works!

I guess the linker did not find the .cpp source file for GraphicsScene::createScene(). And you should add GraphicsScene.cpp to Android.mk file. Such as i did that for my project on windows.
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/Graphic_Image.cpp \
Hope it help.
Thanks.

Related

Linker Error: Undefined reference to `ThreadPool::ThreadPool()'

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.

Building gcc plugin for windows on linux

Try to build the following gcc plugin on linux for windows with mingw cross compiler. The plugins are from the built avr compiler also for windows. Adapted the following plugin https://github.com/jcmvbkbc/avr-flash-vtbl.
#include <gcc-plugin.h>
#include <cp/cp-tree.h>
#ifdef _WIN32
__declspec(dllexport)
#endif
int plugin_is_GPL_compatible = 1;
void fn(void *gcc_data, void *user_data)
{
TYPE_ADDR_SPACE (TREE_TYPE (vtbl_type_node)) = 1;
TYPE_ADDR_SPACE (TREE_TYPE (vtbl_ptr_type_node)) = 1;
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int plugin_init (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version)
{
register_callback("", PLUGIN_START_UNIT, fn, NULL);
return 0;
}
Output during compile and linking:
i686-w64-mingw32-g++ -shared -I/home/andreas/omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/include -Wl,--export-all-symbols /home/andreas/
omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/cc1plus.exe.a avr-flash-vtbl.c -o avr-flash-vtbl.so -I./
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x4): undefined reference to `cp_global_trees'
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x10): undefined reference to `cp_global_trees'
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x44): undefined reference to `register_callback'
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: avr-flash-vtbl.so] Fehler 1
The compiler flags are adapted from https://gcc.gnu.org/onlinedocs/gccint/Plugins-building.html. Has anybody already faced such a issue?
Problem is solved. Changed host compiler from i686-w64-mingw32-g++ to x86_64-w64-mingw32-g++ and changed the order of the options. /home/andreas/omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/cc1plus.exe.a must go after avr-flash-vtbl.c.

C++ Cygwin Undefined reference to constructor

While trying to build my small test I encounter an error I don't understand why it shouldn't work. I'm using Eclipse and Cygwin.
The Header and the Source files are separated into different Folders and I put them also into the Cygwin include folders.
Console log
16:05:41 **** Incremental Build of configuration Debug for project Testarea ****
make all
Building target: Testarea.exe
Invoking: Cygwin C++ Linker
g++ -o "Testarea.exe" ./Source/lint.o ./Source/tester.o
./Source/tester.o: In function `main':
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4: undefined reference to `lint::lint()'
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4:(.text+0x20): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `lint::lint()'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Testarea.exe] Error 1
16:05:41 Build Finished (took 348ms)
tester.cpp
#include <iostream>
#include <lint.h>
int main(){
lint* a = new lint();
std::cout << "hallo";
return 0;
}
lint.cpp
class lint{
private:
int* a;
public:
lint(){
a = new int();
};
lint(int b){
a = new int(b);
};
lint(lint& b){
a = new int(b.value());
};
int value(){
return *a;
};
};
lint.h
#ifndef HEADER_LINT_H_
#define HEADER_LINT_H_
class lint{
public:
lint();
lint(int b);
lint(lint& b);
int value();
};
#endif
Your problem is that you've got 2 classes there. One called lint and the other called lint but only available in the lint.cpp file.
Implementations are done:
#include "lint.h"
lint::lint() {}
and so forth.

C++ Code Blocks error: undefined reference to SDL_PollEvent and WinMain#16

I am getting these 2 errors when trying to create my first SDL application, although I'm not sure if they're related. I've tried to find the reason for each, but nothing I found has helped me. I'm using SDL 2.0.3 and am on Windows 7.
I've looked up the WinMain error and I found it might be trying to compile as windows application rather than console. So I made sure it was console in my project settings and it still gave me that error(though I'm not entirely sure what it's supposed to be set to for an SDL game).
Any related problems to the SDL_PollEvent error I've found are usually linking issues which involve more than just the one line. However, I'm fairly certain I have it linked properly as I did have linking problems previously which I resolved, and I have another reference to SDL_Event which gives no error. This seems particularly strange since both SDL_PollEvent and SDL_Event are in the same header file.
Here is my source and the full error output:
CApp.h:
#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
#include <SDL.h>
class CApp{
private:
bool Running;
public:
CApp();
int OnExecute();
bool OnInit();
void OnEvent(SDL_Event* event);
void OnLoop();
void OnRender();
void OnCleanup();
};
bool CApp::OnInit(){
return true;
}
void CApp::OnEvent(SDL_Event* event){
}
void CApp::OnLoop(){
}
void CApp::OnRender(){
}
void CApp::OnCleanup(){
}
CApp.cpp:
#include "CApp.h"
CApp::CApp(){
Running = true;
}
int CApp::OnExecute(){
if(OnInit() == false){
return -1;
}
SDL_Event event;
while(Running){
while(SDL_PollEvent(&event)){
OnEvent(&event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(){
CApp theApp;
return theApp.OnExecute();
}
Error output:
||=== Build: Debug in CApp (compiler: GNU GCC Compiler) ===|
obj\Debug\CApp.o||In function `ZN4CApp9OnExecuteEv':|
C:\Users\UserName\C++ Projects\Test Platformer\CApp.cpp|17|undefined reference to `SDL_PollEvent'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain#16'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Thanks in advance for any help :)
Edit:
Linker ourput:
mingw32-g++.exe -LC:\SDL\SDL2-2.0.3\lib\x64 -LC:\SDL\SDL2-2.0.3\lib -o "bin\Debug\Test Platformer.exe" obj\Debug\CApp.o -lmingw32 -lSDL2main -lSDL2 -lgdi32
obj\Debug\CApp.o: In function `ZN4CApp9OnExecuteEv':
C:/Users/Zshandi/C++ Projects/Test Platformer/CApp.cpp:17: undefined reference to `SDL_PollEvent'
collect2.exe: error: ld returned 1 exit status
I have now solved my problems, it took me a while to find the solutions. For the WinMain#15 error I was getting, I found out it was because SDL defines it's own main function, so I had to add #undef main to my source. For the other issue I was having, it seems I was using the wrong lib and include files which was giving me the errors, I found I had to use the 32bit mingw files.

Geany simple linking

When I run and build a simple program, it fails.
Here is the error message:
g++ -Wall -o "main" "main.cpp" (in directory: /home/markuz/Desktop)
/tmp/ccHV9wPu.o: In function main':
main.cpp:(.text+0x11): undefined reference toTest::display()'
collect2: ld returned 1 exit status
Compilation failed.
Here are the files. The compile and build command is the default of geany 1.22
//main.cpp
#include "imba.h"
int main(){
Test t;
t.display();
return 0;
}
//imba.h
class Test{
public:
void display();
};
//imba.cpp
#include <iostream>
#include "imba.h"
void Test::display(){
std::cout << "oi";
}
Any ideas about this?
Thanks.
You need to also add the imba.cpp file in the compilation step. Although you have included the header in your main file, you have not compiled the source for it and so the linker cannot find the object file for imba.cpp - that is what the error is complaining about