尽管Alchemy支持编译C++,但似乎使用使用STL很麻烦,主要是由于problem with std::string。奇怪的是,炼金术似乎在使用GNU libstd++ v3.4.6。很难相信std :: string在GNU的STL中被破坏了。使用STL和炼金术
有没有人想出了这个问题的解决方法?没有STL的C++就像没有水的鱼。
尽管Alchemy支持编译C++,但似乎使用使用STL很麻烦,主要是由于problem with std::string。奇怪的是,炼金术似乎在使用GNU libstd++ v3.4.6。很难相信std :: string在GNU的STL中被破坏了。使用STL和炼金术
有没有人想出了这个问题的解决方法?没有STL的C++就像没有水的鱼。
问题不在于STL本身。使用线程安全函数__gnu_cxx::__exchange_and_add
和__gnu_cxx::__atomic_add
对std::string
的GNU实现进行引用计数。问题是__exchange_and_add
/__atomic_add
已损坏。
解决方案是通过良好的这些功能实现来重建STL。
幸运的是,Alchemy发行版为我们留下了一些面包屑。见$ALCHEMY_HOME/avm2-libc/README
,它告诉我们如何做到这一点:
The sources used to build avm2-libstdc++.l.bc can be downloaded here:
http://download.macromedia.com/pub/labs/alchemy/alchemy_gnucpp3-4library_121008.zip
To build avm2-libstdc++.l.bc:
cd $ALCHEMY_HOME/avm2-libc
unzip alchemy_gnucpp3-4library_121008.zip
mv lib/avm2-libstdc++.l.bc lib/avm2-libstdc++.l.bc.OLD
make
You should *not* run achacks prior to using these building this library with make.
The Makefiles provided have been preconfigured to use LLVM directly where needed.
通常我希望找到的实现__exchange_and_add/__ atomic_add在libstd ++($ALCHEMY_HOME/avm2-libc/lib/avm2-libstdc++.l.bc
),但出于某种原因,他们在libc中定义($ALCHEMY_HOME/avm2-libc/lib/avm2-libc.l.bc
) 。
我不确定为什么会出现这种情况,但我们可以通过hacking atomicity.h来解决它,原型保存在那里。请注意,拆包后alchemy_gnucpp3-4library_121008.zip,你需要编辑 atomicity.h文件:
$ALCHEMY_HOME/avm2-libc/include/c++/3.4/bits/atomicity.h
$ALCHEMY_HOME/avm2-libc/libstdc++/include/bits/atomicity.h
下面是代码:
/*
* __exchange_and_add and __atomic_add are broken in Alchemy's libc.
* Replace them with functioning implementations. This isn't
* cross-platform, but this codebase is only compiling for Alchemy anyway.
*/
#define __exchange_and_add(x,y) __exchange_and_add_fix((x),(y))
#define __atomic_add(x,y) __exchange_and_add_fix((x),(y))
/*
* Correctly implement __exchange_and_add. It's not thread-safe,
* but Alchemy isn't threaded, so we should be ok.
*/
inline _Atomic_word __exchange_and_add_fix(volatile _Atomic_word* __mem, int __val) {
int orig= *__mem;
(*__mem)+= __val;
return orig;
}
下面是一些测试代码,以确保重建的STL能够运行:
#include <cstdio>
#include <string>
#include <map>
#include <fstream>
using namespace std;
void string_test() {
string s1;
string s2;
s1 = "a";
s2 = s1; // copy constructor
s1 = "b";
// use your favorite TRACE function here
printf("s1= %s \n", s1.c_str()); // expected: "b", actual: "b"
printf("s2= %s \n", s2.c_str()); // expected: "a", actual: "b", ERROR
}
void map_test() {
map<string, int> test_map;
test_map["test1"]= 1;
test_map["test2"]= 2;
test_map["test3"]= 3;
string tmp= "test1";
printf("%s : %d \n", tmp.c_str(), test_map[tmp]);
}
void ifstream_test()
{
std::ifstream in("test.txt");
// ERROR 1:
// Trying to seek file throws an error:
// Error #1006: value is not a function.
// at: basic_filebuf::char_traits::seekoff
in.seekg(0, std::ios::end);
int length = in.tellg();
in.seekg(0, std::ios::beg);
printf("File Length: %d \n", length);
while(in.good()) {
char buffer[512];
// ERROR 2:
// RangeError: Error #1125: The index 1092156 is out of range 721.
// at basic_filebuf::char_traits::underflow::work()
in.getline(buffer, 512, '\n');
printf("buffer= %s \n", buffer);
}
}
int main() {
string_test();
map_test();
ifstream_test();
return 0;
}
这个问题是我放弃炼金的原因。感谢分享。 – Gunslinger47
@ Gunslinger47:如果你想重建STL,看起来你可能会再次开始使用炼金术。假设你没有转向其他技术。 :) – paleozogt