srcspider
srcspider

Reputation: 11205

Link Error, even though everything is there

I'm working on a (C++) program that has to process a lot of information associated with a file/directory (so more or less I have a path as a key). Currently I've attempted a implementation using hash tables. This seem to work reasonably well given the volume of data but after profiling I find they are still the slowest link in the system, so in order to improve I look into using a Trie.

I find the following (C implementation) http://linux.thai.net/~thep/datrie/datrie.html and reading into the documentation there it seems to be a fairly good. However in attempting to write a simple test snippet I end up with a strange link error.

The strange thing is: the function in question exists (as in its there, and in the cpp file) and the object files are created, so why is there a link error for it?

My code:

#include <iostream>
#include <datrie/trie.h>

extern int main(int, char**)
{
    // create character map covering unicode characters
    AlphaMap *map = alpha_map_new();
    AlphaChar start = 32, end = 1114111;
    alpha_map_add_range(map, start, end);
    // create a trie and test it
    Trie *test = trie_new(map);
    const AlphaChar key[] = {0x64,0x64,0x64,0x64};
    trie_store(test, key, 3);
    TrieData *data;
    trie_retrieve(test, key, data);
    std::cout << *data << std::endl;
    return 0;
}

Error (simplified and line wrapped it for readability)

main.obj : error LNK2001: unresolved external symbol 
"int __cdecl alpha_map_add_range(struct _AlphaMap *,unsigned int,unsigned int)"

main.obj : error LNK2001: unresolved external symbol 
"struct _AlphaMap * __cdecl alpha_map_new(void)"

Using Visual Studio 2010

Upvotes: 1

Views: 548

Answers (2)

nos
nos

Reputation: 229058

You're mixing C and C++. When you include datatrie/trie.h in your C++ code the compiler assumes it's a C++ header, howeever libdatatrie is a C library and its headers arn't C++ friendly.

Include the header like:

extern "C" {
#include <datrie/trie.h>
}

Upvotes: 3

Carl Norum
Carl Norum

Reputation: 224844

There are a few possibilities, but your examples don't give us enough information.

  1. You might not be linking all of the object files together.

  2. You might be compiling one file expecting a C++ name-mangled partner, and the other without. So main.o is looking for the mangled name and the object that exports it is exporting an unmangled name, or vice versa.

Upvotes: 0

Related Questions