使用不在根项目目录上的源文件(包括在搜索路径中的文件)时发生链接错误(Visual C++)

Link error when using source files not on the root project directory (files included in the search paths) (Visual C++)

本文关键字:文件 Visual C++ 错误 链接 路径 包括 项目 源文件 搜索      更新时间:2023-10-16

我一直在编程一个模板结构,在使其工作后,我决定在其他项目中使用它。该模板由两个文件组成。ListOctree.cpp和ListOctree.h。虽然它们自己编译和运行得很好(我可以运行结构自检)。

在其他项目上使用它们时,ListOctree.cpp/.h都在上/Util目录,但尽管Visual Studio似乎找到了.h文件(我可以在项目的任何位置使用.h声明),但它似乎找不到声明.h上声明的所有代码的.cpp源文件。

这些文件是项目的一部分。ists树是这样的:

program.cpp <-- includes "Util/ListOctree.h"
/Util
    ListOctree.cpp
    ListOctree.h

ListOctree中的所有类和函数都属于Util命名空间。

Visual C++抛出的错误为:错误LNK2019:símbolo externo"public:__thiscall Util::ListOctree::ListOctree(int)"(…)sin resolver。。。

停机坪。英语:错误LNK2019:未解析外部符号"public:__thiscall Util::ListOctree::ListOctree(int)"(…)。。。

当重建解决方案时,Util/ListOctre.cpp被编译为生成的.obj文件,但它似乎无法将其链接到

我也可以发布.h文件,但我发现它太长了,现在无法发布。

附件:在哪里以及如何使用.h文件

#include "Util/ListOctree.h"
//...
void main (){
//...
    ListOctree<int>* a = new ListOctree<int>(8);
//...
}

附件:Util/ListOctree.h

#ifndef __UTIL_LISTOCTREE
#define __UTIL_LISTOCTREE
//Conditional compilation flags
#undef _SELFTEST
#undef _DEBUG_LISTOCTREE
//Conditional compilation options
#ifdef _DEBUG_LISTOCTREE
 #undef  _DEALLOCATE_LISTS
 #define _SELFTEST
#endif
#ifdef _SELFTEST
 #include <iostream>
#endif
#ifdef _SELFTEST
 #include<assert.h>
#endif
#ifdef _DEBUG_LISTOCTREE
 #include <iostream>
#endif
/////////////////////////////////
#include <deque>
#include <list>
#include <iterator>
#include <math.h>
#include <exception>
//Remind me to never do this again. (xYz,XYz,XYZ,xYZ,xyz,Xyz,XyZ,xyZ,)
//A CAPS character means positive in the axis while a
//Lowercase character means negative
//FIXME Never used I think.
#define _xyz sons[0]
#define _xyZ sons[1]
#define _xYz sons[2]
#define _xYZ sons[3]
#define _Xyz sons[4]
#define _XyZ sons[5]
#define _XYz sons[6]
#define _XYZ sons[7]
namespace Util{
/**
 * @brief The node class
 *
 * Each node stores up to 8 sons, a parent pointer and, if it
 * is a Leaf node, a list of contents.
 */
template <typename T> class Node{
    //Attributes
public:
    ///An array with all the sons
    Node<T>* sons[8];
    ///A parent pointer
    Node<T>* parent;
    //The node contents (if any)
    std::list<T>* c;
    //Methods
public:
    /// Deallocates itself AND all of it's sons
    ~Node();
    /// Creates an empty node
    Node();
    /// Creates a node with its eight sons and a parent reference from an 9 pointer array
    Node(Node<T>** nodes);
    ///Returns the node contents
    std::list<T>* getContents(){return c;};
    ///Pushes an item into the node
    void pushItem(T item);
    ///Removes an item from the node
    void eraseItem(T item){c->erase(item);};
    ///Clears the node contents
    void clearNode(){c->clear();};
};
/**
 * @brief Container for @see Node classes
 *
 * This class allows a more convenient way of working with Octrees
 */
template <typename T> class ListOctree {
    //Attributes
private:
    int width;
    Node<T>* root;
    //Methods
public:
    ListOctree(int width);
    ~ListOctree();
    void pushItemAt(T item, int x, int y, int z);
    void eraseItemAt(T item, int x, int y, int z);
    void clearItemsAt(int x, int y, int z);
    std::list<T>* getItems();
    std::list<T>* operator() (int x, int y, int z){return(getItemsAt(x,y,z));};
    std::list<T>* getItemsAt(int x, int y, int z);
private:
    char nextNode(int x, int y, int z, int* cx, int* cy, int* cz, int width, Node<T>* n, bool createNew) throw(...);
    void _pushItemAt(T item, int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void _eraseItemAt(T item, int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void _clearItemsAt(int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    std::list<T>* _getItemsAt(int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void prune(Node<T>* n);
    void addItemsToList(std::list<T>* lst, Node<T>* n);
};
/**
 * @brief Exception used internally
 * 
 * FIXME, never used, can't be used for some reason
 */
class ListOctreeException : public std::exception{
private:
    std::string msg;
public:
    ListOctreeException(){msg="ListOctreeException";};
    ListOctreeException(std::string s){msg=s;};
    const char* what(){return(msg.c_str());};
};
};//namespace
#endif

模板的声明和定义应该保存在同一个文件中(通常是.h)。有关详细信息,请参阅常见问题解答(从技术上讲,也可以在包含实现的文件中实例化模板(另请参阅))。

关键字export本应在两者分离的情况下使用,但很少有编译器实现它,现在它已在c++0x中删除。

您是否在解决方案中的项目之间添加了引用?(Visual Studio 2010)

右键单击项目->参考

或者,您是否添加了项目依赖项?(Visual Studio 2008)