clang 应用程序的问题
我正在使用 clang 库构建一个应用程序,我运行到 问题,如果有人能给一些指导,那将会非常有帮助。
#./a.out /home/nmathew/Desktop/algorithms/array.cpp
给出了
In file included from /home/nmathew/Desktop/algorithms/array.cpp:1:
In file included from /usr/include/c++/4.4.3/iostream:39:
In file included from /usr/include/c++/4.4.3/ostream:39:
In file included from /usr/include/c++/4.4.3/ios:38:
In file included from /usr/include/c++/4.4.3/iosfwd:40:
/usr/include/c++/4.4.3/bits/stringfwd.h:49:48: error: unknown type
name 'char_traits'
a.out: TextDiagnosticPrinter.cpp:293: void clang::TextDiagnosticPrinter::EmitCaretDiagnostic(clang::SourceLocation,clang::SourceRange*, unsigned int, const clang::SourceManager&, const
clang::FixItHint*, unsigned int, unsigned int, unsigned int, unsigned
int, unsigned int): Assertion `LangOpts && "Unexpected diagnostic
outside source file processing"' failed.
Stack dump:
0. /usr/include/c++/4.4.3/bits/stringfwd.h:49:48: current parser token
'char_traits'
Aborted (core dumped)
下面的“我的包含”搜索目录。
#include "..."
搜索从这里开始:#include <...>
搜索从此处开始:
/usr/include/linux
/usr/lib/gcc/i686-redhat-linux/4.4.3/include
/usr/include/c++/4.4.3
/usr/include/c++/4.4.3/backward
/usr/include/c++/4.4.3/i686-redhat-linux
/usr/local/include
/usr/include
搜索列表结束。
char_traits.h 位于 /usr/include/c++/4.4.3/bits/ 中,我正在使用 Fedora 12 32位系统。
我的代码列在下面
tut01_pp.cpp
#include "PPContext.h"
int main(int argc, char *argv[])
{
if (argc != 2)
{
return 0;
}
PPContext scope;
scope.headers.PrintStats();
const FileEntry *File = scope.fm.getFile(argv[1]);
if(!File)
{
return 0;
}
scope.sm.createMainFileID(File, SourceLocation());
scope.pp->EnterMainSourceFile();
IdentifierTable identitab(scope.lang);
MinimalAction action(*(scope.pp));
Parser parse(*(scope.pp), action);
parse.ParseTranslationUnit();
identitab.PrintStats();
return 0;
}
PPContext.h
#ifndef PP_CONTEXT
#define PP_CONTEXT
#include <iostream>
#include <string>
using namespace std;
#include <llvm/Support/raw_ostream.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Frontend/Utils.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/HeaderSearchOptions.h>
#include <clang/Parse/Action.h>
#include <clang/Parse/Parser.h>
#include "llvm/System/Host.h"
using namespace clang;
using namespace llvm;
struct PPContext {
PPContext():tdp(ost, options), diag(&tdp), sm(diag), headers(fm)
{
TargetOptions target_options;
target_options.Triple = sys::getHostTriple();
target_options.CXXABI = "itanium";
target = TargetInfo::CreateTargetInfo(diag, target_options);
lang.CPlusPlus = 1;
pp = new Preprocessor(diag, lang, *target, sm, headers);
headeropts.EnvIncPath = "/usr/include/linux";
headeropts.CXXEnvIncPath =
"/usr/lib/gcc/i686-redhat-linux/4.4.3/include/";
headeropts.Verbose = 1;
ApplyHeaderSearchOptions(headers, headeropts, lang,
llvm::Triple(target_options.Triple));
};
~PPContext()
{
delete pp;
delete target;
};
llvm::raw_stdout_ostream ost;
const DiagnosticOptions options;
TextDiagnosticPrinter tdp;
Diagnostic diag;
LangOptions lang;
SourceManager sm;
FileManager fm;
HeaderSearch headers;
TargetInfo *target;
Preprocessor *pp;
HeaderSearchOptions headeropts;
};
#endif //#ifndef PP_CONTEXT
array.cpp
#include <iostream>
#include "array.hpp"
using namespace std;
template <class gen>
ARRAY<gen>::ARRAY()
{
size = MAX;
ptr = (gen (*)[MAX])new gen[size];
}
template <class gen>
ARRAY<gen>::ARRAY(int array_size)
{
size = array_size;
ptr = (gen (*)[])new gen[size];
}
template <class gen>
ARRAY<gen>::~ARRAY()
{
delete (gen (*)[MAX])ptr;
}
template <class gen>
int ARRAY<gen>::getsize()
{
return size;
}
template <class gen>
ARRAY<gen>::ARRAY(const ARRAY &orig)
{
ptr = (gen (*)[MAX])new gen[orig.size];
memcpy(ptr, orig.ptr, sizeof(gen)*orig.size);
size = orig.size;
}
template <class gen>
gen& ARRAY<gen>::operator [](unsigned int index)
{
return *ptr[index];
}
int main(void)
{
ARRAY <int> intarray;
intarray[8] = 16;
return 0;
}
I am building an application using clang libraries, I running to a
problem, it will be very helpful if somebody can give some directions.
#./a.out /home/nmathew/Desktop/algorithms/array.cpp
gives
In file included from /home/nmathew/Desktop/algorithms/array.cpp:1:
In file included from /usr/include/c++/4.4.3/iostream:39:
In file included from /usr/include/c++/4.4.3/ostream:39:
In file included from /usr/include/c++/4.4.3/ios:38:
In file included from /usr/include/c++/4.4.3/iosfwd:40:
/usr/include/c++/4.4.3/bits/stringfwd.h:49:48: error: unknown type
name 'char_traits'
a.out: TextDiagnosticPrinter.cpp:293: void clang::TextDiagnosticPrinter::EmitCaretDiagnostic(clang::SourceLocation,clang::SourceRange*, unsigned int, const clang::SourceManager&, const
clang::FixItHint*, unsigned int, unsigned int, unsigned int, unsigned
int, unsigned int): Assertion `LangOpts && "Unexpected diagnostic
outside source file processing"' failed.
Stack dump:
0. /usr/include/c++/4.4.3/bits/stringfwd.h:49:48: current parser token
'char_traits'
Aborted (core dumped)
My Include search directories are below.
#include "..."
search starts here:#include <...>
search starts here:
/usr/include/linux
/usr/lib/gcc/i686-redhat-linux/4.4.3/include
/usr/include/c++/4.4.3
/usr/include/c++/4.4.3/backward
/usr/include/c++/4.4.3/i686-redhat-linux
/usr/local/include
/usr/include
End of search list.
and char_traits.h is in /usr/include/c++/4.4.3/bits/, I am using
Fedora 12 32bit system.
My code is listed below
tut01_pp.cpp
#include "PPContext.h"
int main(int argc, char *argv[])
{
if (argc != 2)
{
return 0;
}
PPContext scope;
scope.headers.PrintStats();
const FileEntry *File = scope.fm.getFile(argv[1]);
if(!File)
{
return 0;
}
scope.sm.createMainFileID(File, SourceLocation());
scope.pp->EnterMainSourceFile();
IdentifierTable identitab(scope.lang);
MinimalAction action(*(scope.pp));
Parser parse(*(scope.pp), action);
parse.ParseTranslationUnit();
identitab.PrintStats();
return 0;
}
PPContext.h
#ifndef PP_CONTEXT
#define PP_CONTEXT
#include <iostream>
#include <string>
using namespace std;
#include <llvm/Support/raw_ostream.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Frontend/Utils.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/HeaderSearchOptions.h>
#include <clang/Parse/Action.h>
#include <clang/Parse/Parser.h>
#include "llvm/System/Host.h"
using namespace clang;
using namespace llvm;
struct PPContext {
PPContext():tdp(ost, options), diag(&tdp), sm(diag), headers(fm)
{
TargetOptions target_options;
target_options.Triple = sys::getHostTriple();
target_options.CXXABI = "itanium";
target = TargetInfo::CreateTargetInfo(diag, target_options);
lang.CPlusPlus = 1;
pp = new Preprocessor(diag, lang, *target, sm, headers);
headeropts.EnvIncPath = "/usr/include/linux";
headeropts.CXXEnvIncPath =
"/usr/lib/gcc/i686-redhat-linux/4.4.3/include/";
headeropts.Verbose = 1;
ApplyHeaderSearchOptions(headers, headeropts, lang,
llvm::Triple(target_options.Triple));
};
~PPContext()
{
delete pp;
delete target;
};
llvm::raw_stdout_ostream ost;
const DiagnosticOptions options;
TextDiagnosticPrinter tdp;
Diagnostic diag;
LangOptions lang;
SourceManager sm;
FileManager fm;
HeaderSearch headers;
TargetInfo *target;
Preprocessor *pp;
HeaderSearchOptions headeropts;
};
#endif //#ifndef PP_CONTEXT
array.cpp
#include <iostream>
#include "array.hpp"
using namespace std;
template <class gen>
ARRAY<gen>::ARRAY()
{
size = MAX;
ptr = (gen (*)[MAX])new gen[size];
}
template <class gen>
ARRAY<gen>::ARRAY(int array_size)
{
size = array_size;
ptr = (gen (*)[])new gen[size];
}
template <class gen>
ARRAY<gen>::~ARRAY()
{
delete (gen (*)[MAX])ptr;
}
template <class gen>
int ARRAY<gen>::getsize()
{
return size;
}
template <class gen>
ARRAY<gen>::ARRAY(const ARRAY &orig)
{
ptr = (gen (*)[MAX])new gen[orig.size];
memcpy(ptr, orig.ptr, sizeof(gen)*orig.size);
size = orig.size;
}
template <class gen>
gen& ARRAY<gen>::operator [](unsigned int index)
{
return *ptr[index];
}
int main(void)
{
ARRAY <int> intarray;
intarray[8] = 16;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表明模板类“char_traits”不存在。这个实际上是在“char_traits.h”中定义的,不包含在“array.cpp”中。
虽然在这个文件中,它写着:
但你仍然可以尝试包含它并使 clang 高兴。
The error shows that template class "char_traits" doesn't exist. This one, actually is defined in "char_traits.h", is not included by "array.cpp".
Although in this file, it reads:
But you can still try to include it and make clang happy.