undefined reference to namespaced globals??

support for the ARM toolchain
Post Reply
capz
Posts: 13
Joined: Sat Nov 29, 2014 3:07 am

undefined reference to namespaced globals??

Post by capz » Sat Nov 29, 2014 3:24 am

Hi all, great to see so many people into homebrew on here :)

With the 3DS scene kind of exploding lately, I've decided to see if i can port a C++11 project of mine to the 3DS.
I'm running into the weirdest issue, however..

the following produces errors for me, for some reason. Anyone care to take a look at it? Because I've tried everything I can come up with..
builds fine on windows using Dev-C++(GCC4.8.1), and OSX using Xcode(LLVM6.0/Clang-600.0.054)
on using devkitARM, i'm getting:

Code: Select all

linking tuto.elf
main.o: In function `main':
c:/3dsdev/tuto/source/main.cpp:71: undefined reference to `pt::input::kControllerButtonPressEvent'
collect2.exe: error: ld returned 1 exit status
make[1]: *** [/c/3dsdev/tuto/tuto.elf] Error 1
make: *** [build] Error 2

Code: Select all

//InputEnums.hpp
#pragma once
namespace pt {
	namespace input {
		//controller
		extern const unsigned long kControllerButtonPressEvent;
	}
}

Code: Select all

//InputEnums.cpp
#include "InputEnums.hpp"
namespace pt {
	namespace input {
		//controller
		const unsigned long kControllerButtonPressEvent = 10010;
	}
}

Code: Select all

//main.cpp
#include <iostream>
#include "input/InputEnums.hpp"
int main()
{
	auto t = pt::input::kControllerButtonPressEvent;
	std::cout << t << std::endl;
	return 0;
}

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: undefined reference to namespaced globals??

Post by WinterMute » Sat Nov 29, 2014 3:50 am

You're either not linking InputEnums.o or not compiling InputEnums.cpp in the first place.

It's probably easier to help if you stick the project with these 3 files on github or similar. I imagine the trouble is in your Makefile
Help keep devkitPro toolchains free, Donate today

Personal Blog

capz
Posts: 13
Joined: Sat Nov 29, 2014 3:07 am

Re: undefined reference to namespaced globals??

Post by capz » Tue Dec 09, 2014 5:40 am

Turns out that was the problem indeed. I'm totally not used to makefiles and still am clueless but figured out that the provided makefile for ctrulib-based projects doesn't traverse into subfolders when building source files, so it wasn't compiling my neatly organized code. Kind of a pain but not so hard to fix by sticking all my code in the same place.

Would it be possible to have directory structure traversal happen when the makefile is looking for source files? I'm using an unmodified version of the ctrulib makefile. I can paste it somewhere if that would make things easier..

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: undefined reference to namespaced globals??

Post by WinterMute » Sun Dec 14, 2014 10:05 am

You can either add all the folders containing source files to the SOURCES line in the Makefile like so

Code: Select all

SOURCES := source source/subdir1 source/subdir2
or, if you just want it to compile everything in there you can do this

Code: Select all

SOURCES := $(shell find source -type d)
although that will show an error when make recurses into the build folder so you can do this instead

Code: Select all

ifneq ($(BUILD),$(notdir $(CURDIR)))

SOURCES	:=	$(shell find source -type d)
INCLUDES	:=	include fonts
DATA		:=	data

endif
which will stop those variables being set a second time
Help keep devkitPro toolchains free, Donate today

Personal Blog

capz
Posts: 13
Joined: Sat Nov 29, 2014 3:07 am

Re: undefined reference to namespaced globals??

Post by capz » Sun Sep 06, 2015 12:57 am

thanks for the reply!
I can finally say thanks now, somehow the forum is set to not allow anyone from anywhere in asia to log in, and I've been there for months. troublesome!
Also, I must say I've come to appreciate the similarities in the various devkitpro sdks. in terms of design and structure :mrgreen:

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests