C++:Undefined reference to 'FMOD:: X' - c++

After looking around for various sound API libraries, I have decided to use FMOD for the time being.
Problem is that whenever I try to compile one of the code examples, I get the following errors:
obj\Release\main.o:main.cpp|| undefined reference to `FMOD::System::getVersion(unsigned int*)#8'|
obj\Release\main.o:main.cpp|| undefined reference to `FMOD::System::init(int, unsigned int, void*)#16'|
obj\Release\main.o:main.cpp|| undefined reference to `FMOD::System::createSound(char const*, unsigned int, FMOD_CREATESOUNDEXINFO*, FMOD::Sound**)#20'|
obj\Release\main.o:main.cpp|| undefined reference to `FMOD::Sound::setMode(unsigned int)#8'|
The code example that I am using being this:
#include <D:\Games\FMOD Programmers API Win32\api\inc\fmod.hpp>
#include <D:\Games\FMOD Programmers API Win32\api\inc\fmod_errors.h>
#include <sstream>
#include <windows.h> // for PlaySound()
#include <time.h>
#include <mmsystem.h>
using namespace std;
int main(int argc, char* argv[])
{
FMOD::System *system;
FMOD::Sound *sound1, *sound2, *sound3;
FMOD::Channel *channel = 0;
FMOD_RESULT result;
int key;
unsigned int version;
/*
Create a System object and initialize.
*/
result = FMOD::System_Create(&system);
result = system->getVersion(&version);
result = system->init(32, FMOD_INIT_NORMAL, 0);
result = system->createSound("../media/drumloop.wav", FMOD_HARDWARE, 0, &sound1);
result = sound1->setMode(FMOD_LOOP_OFF); /* drumloop.wav has embedded loop points which automatically makes looping turn on, */
/* so turn it off here. We could have also just put FMOD_LOOP_OFF in the above CreateSound call. */
// Code continues into other bits that work...
I am using the latest version of FMOD and am using the Code::Blocks IDE (ver 10.05), with the GNU GCC compiler. The project is of type "Console application". The fmodex.dll file is in the folder of my project. I am using windows XP 32 bit SP3.
I have linked to the libfmodex.a library and have tried linking to the other libraries it has there as well, but this does not solve the problem.
My question is, therefore, what do I need to do to stop these errors occurring? As when I encountered similar "Undefined reference to x" errors before using other libraries. I had just forgotten to link to them in Code::Blocks and as soon as I did, they would work.
Do say if you need more information regarding the code etc.

When using FMOD with Code::Blocks you need to use the C API, not the C++ API. FMOD is built with Visual Studio, therefore the C++ symbols use the VC mangling scheme. There is a note in the "Getting Started with FMOD for Windows" document that mentions this.
http://en.wikipedia.org/wiki/Name_mangling#How_different_compilers_mangle_the_same_functions

I do not have a Windows box ready to verify this on, but try replacing those backslashes with forward slashes in the include paths, or escape the backslashes.
#include <D:/Games/FMOD Programmers API Win32/api/inc/fmod.hpp>
#include <D:/Games/FMOD Programmers API Win32/api/inc/fmod_errors.h>
or
#include <D:\\Games\\FMOD Programmers API Win32\\api\\inc\\fmod.hpp>
#include <D:\\Games\\FMOD Programmers API Win32\\api\\inc\\fmod_errors.h>
(Or, better, just add D:\Games\FMOD Programmers API Win32\api\inc\ to your list of include paths, and include the files by filename instead of full path; then your code might actually compile somewhere other than your specific computer!)

Those undefined reference errors mean that the compiler, or rather the linker part of the compiler, cannot find the library.
I don't use Code::Blocks so I don't know where the setting is, but you need to tell your project to use the library and where to find it.
Just putting the DLL in the directory is enough for running the program, but for linking it you need a .lib file.

Related

TDM-GCC raises error: Undefined reference to a method I never called

This is with the compiler that comes with Dev-C++ when run on Windows 7, "TDM-GCC 4.9.2 64-bit Release". The same thing happens with the 64-bit version as well. The "add the following flags when calling the linker" in "Compiler Options" is set to -static-libgcc -lws2_32, and "add the following flags when calling the compiler" is std=c++11.
Unfortunately, I can't get much more information than that because this is a very, very locked down computer.
Once I get home, I'll test it there and edit in any new information.
A test file:
#include <windows.h>
#include <psapi.h>
#include <iostream>
int main() {
HANDLE h = GetCurrentProcess();
TCHAR name[100];
GetProcessImageFileName(h, name, 100);
std::cout << "Name is " << std::endl;
}
When compiled, it gives the following error:
C:\Users\[REDACTED]\AppData\Local\Temp\ccQz3L9P.o:test.cpp:(.text+0x2f): undefined reference to `GetProcessImageFileNameA'
As best as I can tell, it's calling GetProcessImageFileNameA instead of GetProcessImageFileName because of a #define in psapi.h:
#define GetProcessImageFileName __MINGW_NAME_AW(GetProcessImageFileName)
__MINGW_NAME_AW(func) is #defined in _mingw_unicode.h as func##A, which if I understand it correctly is where the new method name is coming from.
Back in psapi.h, DWORD WINAPI GetProcessImageFileNameA(HANDLE, LPSTR, DWORD); is declared, but I haven't found its definition anywhere, and Dev-C++ can't either. I'm fairly certain that's the problem.
How can I fix it? I'm open to anything, up to and including switching compilers or IDEs, as long as I can put whatever I use on a flash drive and carry it around with as little annoyance as possible, and that the end result doesn't require admin privileges to run.
GetProcessImageFileName is a windows API function and you need to link in the correct library to get to it.
It is defined as a macro to be either GetProcessImageFileNameA or GetProcessImageFileNameW depending whether you are compiling "wide" or "narrow".
The Win32 API has Unicode and ascii versions of each function (at least the functions that take strings). In general, the ascii version converts the characters to Unicode and calls the other implantation internally.
you need to link Psapi.lib or Kernel32.lib into your project. I am not sure about your compiler but in VC++ you add #pragma comment(lib, "Psapi.Lib") to your code or add Psapi.lib in additional libraries of project properties.
Sam

How to use a C struct in C++ code?

I am trying to write a program that should use a C library (the LIS library) in a C++ program. There seems to be a problem with the creation/initialization of struct objects.
When I run the example program on the wikipediapage: http://en.wikipedia.org/wiki/Lis_%28linear_algebra_library%29 it runs like a charm, but of course that is compiled as a C program.
In my C++ code I do it as follows:
#include "stdafx.h"
#include <iostream>
extern "C"
{
#include "lis.h"
#include "lis_config.h"
LIS_MATRIX A;
}
using namespace std;
int main(LIS_INT argc, char* argv[])
{
lis_initialize(&argc, &argv);
lis_matrix_create(LIS_COMM_WORLD, &A);
getchar();
return 0;
}
When I run this code, it gives me an access violation at the line lis_matrix_create. It seems as though A has an memory address, its data members (LIS_MATRIX is defined as a struct in Lis.h) have not been initialized, and therefore their addresses are NULL.
Could you please tell me how to create the LIS_MATRIX in such a way that I can use it like it is done in the example code on the wikipedia page?
Thank you in advance!
In reply to Adam and Ross Ridge:
I use visual studio 2013 on Windows 7 64 bit. The manual of the Lis library states that it is compatible with the Visual Studio 2008, 2010 and 2012 compilers, and also with gcc 3.4 and 4.4 and some IBM, Intel and PGI C++ compilers, I hope Visual Studio 2013 will not be a problem.
Also, in this code, if I take out the 'extern C' block, and include 'stdio.h' instead of iostream, it runs without problems (so I guess that it means the C compiler is used?). The minute I also include iostream, the access violation start.
You are including
lis_config.h
after
lis.h
wich is per se an error(you have to include it before). Also if you touched anything in lis_config you have to rebuild the whole library (using most same compilers flag of your project, for example "-msee2" if you used SSE2). Before rebuilding just swap headers only to see if that is enough..
A few more words: a library can easily detect headers included in wrong order, make a ticket to lis developers for that.

undefined reference to boost::gregorian::greg_month::as_short_string() const

This was asked several times however I don't know what I'm doing wrong. I'm trying to get the current date subtracted by 7. Here's the Main:
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/date_formatting.hpp>
#include <boost/date_time/gregorian/greg_month.hpp>
using namespace std;
using namespace boost::gregorian;
int main(int argc, char **argv) {
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
date cdate(timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday);
cdate += date_duration(-7);
string date = to_iso_string(cdate);
cout << date << endl;
return 0;
}
When I try to compile it I get the following error.
E:/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
E:/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
Can anyone help? I thought I included the neccessary files..
Boost date_time is not a header-only library. Please build the library and then add it. Simple in gcc:
gcc myapp.cpp -omyapp -lboost_date_time
(Be careful! This library sneakily appears to work as a header-only library at optimization levels -O2 and higher, due to inlining; but it will fail to link when you use lower optimization levels where the compiler's inliner isn't as aggressive.)
I think the compiler is complaining about the inclusion of boost lib.
In order to use boost::gregorian(boost::date_time), you need to use
bjam to build boost library and then link it against the FileSystem lib.
The reference of boost see click here.
EDIT: According to what you got above, the problem is that the library can't be found, mingw seems like don't know where it is. A re-installation of mingw maybe required or you can try to specify the specific path of the library.
Good luck!
you should add the link lib named
libboost_date_time-mgw46-d-1_54.dll.a
(my path D:\My Documents\Downloads\boost_1_54_0\bin.v2\libs\date_time\build\gcc-mingw-4.6.2\debug\libboost_date_time-mgw46-d-1_54.dll.a) to the compiler's path
Good luck
The reason of the linking issue is the class grep_month part of implementation is at other cpp file located in file boost_xxx_xx_x\libs\date_time\src\gregorian\greg_month.cpp. So this should be built into a static library or directly built into your target.
The other reason of why the "Release" mode with option "O2" building could pass ok, it should be caused by the final codes has not called the gregorian::greg_month related codes, and the complier ignore linking the unused function into the target, so the building is sneakily passed.
So the CyberGuy's comments in the stackoverflow website about the argument of inlining should just be a guess.

Simple C++ program using pqxx (postgres)

I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. Here's the code.
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
int main()
{
pqxx::connection MyConn ("dbname=dbESM user=postgres");
cout << "Hello world!" << endl;
return 0;
}
But I get the following error on hitting F9 to compile and run:
/usr/include/pqxx/connection.hxx|87|undefined reference to
`pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'
The above message is from the file connection.hxx and the line highlighted is this:
explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}
The connection.hxx file is not mine - I think it's part of pqxx.
I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.
You need to add the reference to the libpqxx library to the project.
Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options, then open the tab called Linker settings, then hit Add, then enter pqxx.
If you were using the libpq C library instead, the procedure would be identical except the name would be pq.
You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.

How to link an archive without depending on an IDE?

I've been using a TCP sockets tutorial for C++, and came across the following line -
Before we begin, you will need to include winsock.h and link libws2_32.a to your project in order to use the API that are necessary for TCP/IP. If this is not possible, use LoadLibrary() to load ws2_32.dll at runtime, or some similar method.
After some research, I found a source that claimed this could be done with the following two lines -
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
However, one of the two seem to not be working (I assume that it would be the second line, as the first is fairly straightforward). Here is the code I am trying to run -
#include <iostream>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
std::cout << gethostbyname("www.google.com") << std::endl;
return 0;
}
This code, which should be printing the IP address of Google, instead gives the following error -
C:\Users\Owner\AppData\Local\Temp\ccOXoE0F.o:SC.cpp:(.text+0x1e): undefined reference to 'gethosebyname#4'
collect2: ld returned 1 exit status
Some more googling revealed that the reason for this is an improper linking of the library. According to these sources, the only way to link this is through an IDE. I generally use Notepad++ along with the command prompt, and so I ask if it is possible to properly link this archive without using an IDE.
If it isn't possible, then it looks like I'll have to get back into using IDEs.
No, IDEs have nothing to do with this, and your desire is correct and entirely understandable. Since you seem to be on Windows, I'm not sure what kind of command-line toolchain you are using, but if it's one of the GNU-compatible ones (GCC with MinGW, for example), then you can specify the -lws2_32 linker flag to link against the library.