Using SFML on MinGW - c++

I've been working on a Gameboy emulator, and everything's looking great, I've already implemented the core, flag updates, cycle accurate interrupts, etc. I thought it was time to see something.
I chose SFML for the graphics, and, so far I was able to compile the example program. But, when I tried to compile my emulator, it seems that the actual C++ standard library was unlinked, as the log reports undefined references from STD methods, such as:
agb\main.o:main.cc:(.text+0x91): undefined reference to `__ZSt4cout'
agb\main.o:main.cc:(.text+0x120): undefined reference to `__ZNSt8ios_base4InitD1Ev'
agb\main.o:main.cc:(.text+0x141): undefined reference to `__ZNSt8ios_base4InitC1Ev'
and, this particularly interesting one:
C:/Users/Astherix/Desktop/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
Anyone has a clue as to why this is happening?
I use a custom BAT to compile, this is the source:
#echo off
set name=%1
echo Started building project '%name%' through gcc.
echo Creating object file '%name%.o'...
g++ -c %name%.cc -IC:\Users\Astherix\Desktop\mingw64\sfml\include
echo Compiling executable '%name%.exe'...
g++ %name%.o -o %name%.exe -LC:\Users\Astherix\Desktop\mingw64\sfml\lib -lsfml-graphics -lsfml-window -lsfml-system
Thanks in advance.

If the source code is within a folder, you might want to move it to the same folder g++, c++ or whatever compiler you're using is in. Or just make a copy-move script of sorts.

Related

c++ undefined reference to PathFileExistsW (_imp__PathFileExistsW#4)

I'm trying to build headless blackcoin from source on Windows 8 with mingw 6.3
and I got the following error: .../src/leveldb/libleveldb.a(env_win.o):env_win.cc:(.text+0xaff): undefined reference to '_imp__PathFileExistsW#4'
This function is mentioned here https://msdn.microsoft.com/en-us/library/windows/desktop/bb773584(v=vs.85).aspx
I would appreciate if someone can help me.
P.S. I linked the appropriate library.
Finally I was able to build it. Probably it is obvious for thoose guys who down voted the question. I simply answer since it might save time to somebody.
I relied on the Makefile which is a part of the project source code. Required library (shlwapi) present in the file so I was confused when it failed to build. Everything went well when I placed the library at the end of the linked files.
It was something like this
g++ ... -o -lshlwapi... /src/leveldb/libleveldb.a /src/leveldb/libmemenv.a
And became this
g++ ... -o /src/leveldb/libleveldb.a /src/leveldb/libmemenv.a -lshlwapi

Unable to link CCfits example program

This is probably related to
c++ reading fits file using ccfits
which was never answered.
Anyway, I hope my question is easier to reproduce. There is an example program for CCfits at:
http://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/cookbook.html
I am attempting to compile this using:
g++ cookbook.cpp -o cookbook -lCCfits -lcfitsio
The link fails for every CCfits function in the file:
/tmp/cc7hVaju.o: In function main':
cookbook.cpp:(.text+0x14): undefined reference towriteImage()'
cookbook.cpp:(.text+0x31): undefined reference to writeAscii()'
cookbook.cpp:(.text+0x4e): undefined reference towriteBinary()'
cookbook.cpp:(.text+0x6b): undefined reference to copyHDU()'
cookbook.cpp:(.text+0x88): undefined reference toreadHeader()'
cookbook.cpp:(.text+0xa5): undefined reference to readImage()'
cookbook.cpp:(.text+0xc2): undefined reference toreadTable()'
cookbook.cpp:(.text+0xdf): undefined reference to readExtendedSyntax()'
cookbook.cpp:(.text+0xfc): undefined reference toselectRows()'
collect2: error: ld returned 1 exit status
I have tried this with the CCfits package that comes with Ubuntu. I have also tried installing the package myself. Same error.
Strangely, I get similar messages if I do not include the libraries on the command line (i.e., "g++ cookbook.cpp -o cookbook"). The one difference is that I also get this error:
/tmp/ccMVMkSB.o: In function CCfits::FITS::setVerboseMode(bool)':
cookbook.cpp:(.text._ZN6CCfits4FITS14setVerboseModeEb[_ZN6CCfits4FITS14setVerboseModeEb]+0xf): undefined reference toCCfits::FITS::s_verboseMode'
This must be a clue, right? Seems to say that the libraries I have named, although they exist, do not contain all the functions I need.
Thanks for any help,
Charles
Not sure if you got a suitable answer to this question but as far as I can tell the main issue is that you are not including the definitions to the function signatures. These are usually defined in the header files of c++ libraries.
For example, if your library is installed in "/usr/local" on a UNIX system then the header files will be installed in the location "/usr/local/include/CCfits". The corresponding lib files will be installed at "/usr/local/lib". The important thing is that the compiler does not know this and you need to inform it of these locations.
g++ cookbook.cpp -o cookbook -I /usr/local/include/CCfits -L /usr/local/lib -lCCfits -lcfitsio
The "-I /usr/local/include/CCfits" flag and the given parameter inform g++ of the location of the header files that it is looking for. The "-L /usr/local/lib" flag and the given parameter inform g++ of the location of the library files. It is important to note that g++ will search in the standard location for libraries on in your environment as well this is just giving it more locations to search. There are in fact rules for what it should do if it finds multiple libraries which are the same in different locations but I don't explicitly remember those.
Also to be safe, ensure that the libraries are loaded into memory by the OS. These are shared libraries not static so they are not stored into the executable file. This won't make a difference when compiling the source but will prevent the successful execution of the executable. To ensure that the OS has loaded the library into memory run the following command:
sudo ldconfig
Yours Aye,
Omar EQ

Including expect/tcl library for C/C++

Recently I found an example of how to use the expect library in C++. I tried to compile it, but the compiler (g++) said, that tcl8.5/expect.h doesn't exists. So I tried to include tcl8.6/expect.h - still the same error. I checked the /usr/include/ directory and I wasn't surprised when I've noticed, that there is no tcl8.x directory.
I've searched for files with "expect" in their name. Here's what I found:
/usr/include/expect_tcl.h
/usr/include/expect_comm.h
/usr/include/expect.h
Unfortunately when I tried to include any of these I got the following list of errors during compilation:
> g++ test.cpp -Wall -std=c++0x -ltcl8.6 -lglog -o test
/tmp/cce8k1BA.o: In function `task(std::string const&, std::string const&, std::string const&)':
test.cpp:(.text+0x16): undefined reference to `exp_is_debugging'
test.cpp:(.text+0x20): undefined reference to `exp_timeout'
test.cpp:(.text+0x38): undefined reference to `exp_popen'
etc...
How can I solve this problem?
[EDIT]
When I tried to link it with the expect lib (-lexpect) I got the following error:
/usr/bin/ld: cannot find -lexpect
collect2: error: ld returned 1 exit status
I'm sure that both - tcl8.6 and expect 5.45-4 are installed.
The usual way of distributing Expect these days puts the shared library in a non-standard location and loads it dynamically by full pathname. This works well and is minimal fuss for most people, but does make it rather hard to use Expect's C interface in your own code.
The easiest way is going to be to build your own copy from source, especially as that will give you control over how exactly it was built. This can particularly include keeping the majority of symbols in the library instead of stripping them on install, which will help a lot with debugging. You probably ought to use the current supported version. (Yes, it's a release from several years ago. It doesn't need a lot of support effort most of the time.)
You haven't linked to the expect library during your build. Add -lexpect to your g++ command.

OpenCV compile error undefined reference to `cv::createEigenFaceRecognizer(int, double)'

I've been trying to figure this out for three days, but I'm quite new to C++ and I seem to have hit a wall.
I want to change one of the opencv source files, particularly the eigenfaces function written by bytefish. I want to have it return many faces instead of one. I believe I figured this part out.
The problem is that, when I recompile opencv, no matter what dll I use, when compiling the demo facial recognition code, I get a couple errors:
SET: IncDir = C:\opencv\build\include
$(INCDIR) = C:\opencv\build\include
SET: LibDir = C:\opencv\build\x86\mingw\lib
$(LIBDIR) = C:\opencv\build\x86\mingw\lib
g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" facerec_eigenfaces.cpp -lopencv_calib3d243 -lopencv_contrib243 -lopencv_core243 -lopencv_features2d243 -lopencv_flann243 -lopencv_gpu243 -lopencv_highgui243 -lopencv_imgproc243 -lopencv_legacy243 -lopencv_ml243 -lopencv_nonfree243 -lopencv_objdetect243 -lopencv_photo243 -lopencv_stitching243 -lopencv_video243 -lopencv_videostab243 -o facerec_eigenfaces
Process started >>>
C:\Users\Nate\AppData\Local\Temp\ccDRRGhr.o:facerec_eigenfaces.cpp:(.text+0x8a0): undefined reference to `cv::createEigenFaceRecognizer(int, double)'
C:\Users\Nate\AppData\Local\Temp\ccDRRGhr.o:facerec_eigenfaces.cpp:(.text+0xdf5): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
collect2: ld returned 1 exit status
<<< Process finished.
SET: Libs = -lopencv_calib3d243 -lopencv_contrib243 -lopencv_core243 -lopencv_features2d243 -lopencv_flann243 -lopencv_gpu243 -lopencv_highgui243 -lopencv_imgproc243 -lopencv_legacy243 -lopencv_ml243(...)
$(LIBS) = -lopencv_calib3d243 -lopencv_contrib243 -lopencv_core243 -lopencv_features2d243 -lopencv_flann243 -lopencv_gpu243 -lopencv_highgui243 -lopencv_imgproc243 -lopencv_legacy243 -lopencv_ml243 -lopen(...)
facerec_eigenfaces
I have seen a couple similar problems, and I have tried implementing all of the methods listed therein, to no avail. This is my first C++ project, so I may be missing something basic, but it looks like by using all of the opencv guides, it builds the contrib dll improperly.
If I need to include anything else here I will. I hate this.
Update
I got this portion of it working. It appears as though I was mixing and matching dll files built with Visual C, with non-visual C built files, which I wouldn't think would matter, but regardless, the code works.
Basically I had been using Notepad++ as my IDE, configured to compile using G++ and MinGW, and then I changed some files with visual C and tried to insert those into my previous project. Then I recompiled with Notepad++ and it would bomb.
So to make it work, I used Visual C as my sole editor. I built it following the instructions from the OpenCV install guide (google), and made my changes only with Visual C. Then I compiled with Visual C too.
I am a C++ newb, so if anyone wants to give a better answer as to what is going on, I'll vote it the best answer.

Undefined reference to operator new

I'm trying to build a simple unit test executable, using cpputest. I've built the cpputest framework into a static library, and am now trying to link that into an executable. However, I'm tied into a fairly complicated Makefile setup, because of the related code.
This is my command line:
/usr/bin/qcc -V4.2.4,gcc_ntoarmle_acpp-ne -lang-c++ -O2 -g -g -o Application/UnitTests/Tests/symbols/UnitTestExe -Wl,--start-group Application/UnitTests/Tests/../.objs/main.o Application/UnitTests/lib/libcpputest.a -Wl,--end-group -lm
I'm getting many errors like the following:
Application/UnitTests/lib/libcpputest.a(CommandLineTestRunner.o): In function `CommandLineTestRunner::parseArguments(TestPlugin*)':
Application/UnitTests/cpputest/src/CppUTest/.objs/../CommandLineTestRunner.cpp:114: undefined reference to `operator new(unsigned int, char const*, int)'
I can't figure out what's causing this. Don't I get operator new for free with C++?
You probably need to link with the C++ support runtime library. This happens automatically when you invoke g++. On Linux, this is achieved by adding the -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.
Maybe you're calling gcc, the C compiler instead of g++, which is the C++ compiler.
There's very little information in your question to work from, but it looks like some code uses some form of placement new, and while that special operator new is declared (the compiler finds it and compiles the code using it), the linker can't find its definition.
(Since this old answer of mine seems to still get attention: See here for an extensive discussion on declaration vs. definition.)
You need to rebuild your code from scratch, including the library. I got this error because I inadvertently copied object files compiled on another machine (with the rest of the source) to my machine. Most likely this disturbs the linking step since there are now two types of object files, native (for modified source files) and non-native (all others). I am guessing here, but the operator 'new' means slightly different things on different architectures and that's why you are getting this error.
p.s. I know this is way too late for a useful answer but I'm still posting this for the record.
For QNX 6.5.0 I have specified flag -lang-c++ for qcc (gcc) to avoid the error.
Like the original post, in my case this error happened while trying to link a software using CppUTest framework.
In my case, the source of the problem seems to be related to the fact I disabled the MEMORY_LEAK_DETECTION compile option of CppUTest. I enabled it again, which solved the problem.
Sometimes adding -lstdc++ is not enough. You should add it to the right place. For example I had list like this, not working:
target_link_libraries(cfr2 pthread m stdc++ "${CMAKE_SOURCE_DIR}/compressor/libcompressor.a" )
But this one works fine:
target_link_libraries(cfr2 pthread m "${CMAKE_SOURCE_DIR}/compressor/libcompressor.a" stdc++)
It'd be great if someone explained it in the comment section.