在共享库中使用静态std::string时出现问题 我试图在QT应用程序中使用C++库进行机械模拟。 在我尝试使用库的某些部分并得到一些错误之前,一切都正常。问题代码的一部分是:
ChGlobal.h在共享库中使用静态std::string时出现问题 我试图在QT应用程序中使用C++库进行机械模拟。 在我尝试使用库的某些部分并得到一些错误之前,一切都正常。问题代码的一部分是:,c++,qt,C++,Qt,ChGlobal.h // // PROJECT CHRONO - http://projectchrono.org // // Copyright (c) 2010-2011 Alessandro Tasora // All rights reserved. // // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file at the top le
//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010-2011 Alessandro Tasora
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//
#ifndef CHGLOBAL_H
#define CHGLOBAL_H
#include <string>
#include "core/ChApiCE.h"
namespace chrono {
/// Set the path to the Chrono data directory (ATTENTION: not thread safe)
ChApi void SetChronoDataPath(const std::string& path);
/// Obtain the current path to the Chrono data directory (thread safe)
ChApi const std::string& GetChronoDataPath();
/// Obtain the complete path to the specified filename, given relative to the
/// Chrono data directory (thread safe)
ChApi std::string GetChronoDataFile(const std::string& filename);
} // END_OF_NAMESPACE____
#endif
我只得到绳子的一部分
然后,如果尝试使用代码设置新路径:
const std::string& newpath("c:/");
chrono::SetChronoDataPath(newpath);
我得到以下错误:
Exception at 0x74dfc42d, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\crtw32\heap\new.cpp:62
是因为时间数据路径是一个静态变量吗?我尝试过其他方法,但都不管用。我不是C++专家,也许我错过了什么,
谢谢最简单的解决方案是转换库并将其与应用程序静态链接 如果您坚持动态链接库,则可以使用线程安全的Q_GLOBAL_静态机制:
// implementation (.cpp)
class chrono_data_path_t : public std::string {
public:
chrono_data_path_t() : std::string("../data/") {}
};
Q_GLOBAL_STATIC(chrono_data_path_t, chrono_data_path)
namespace chrono {
void SetChronoDataPath(const std::string& path) {
*chrono_data_path = path;
}
const std::string& GetChronoDataPath() {
return *chrono_data_path;
}
std::string GetChronoDataFile(const std::string& filename) {
return *chrono_data_path + filename;
}
} // namespace chrono
我解决了这个问题,我把程序的调试版本和库的发布版本混合在一起。库的Cmake配置正在为两个生成版本生成相同的名称 如果将static std::string chrono_data_path../data/,问题可能会得到解决;在行返回chrono_data_路径之前;请试试这个。@Tarod:你的意思是让它成为该函数的局部函数?这会让事情变得更糟:其他两个函数无法访问它。我测试了,也遇到了同样的问题…尝试将chrono_data_路径声明包含到头文件中。可能是它没有正确初始化。@Amartel:不,这会让事情变得更糟:在每个包含标题的翻译单元中都有一个单独的实例。但是该库是在visual studio 2013中编译的,我不能使用Q_GLOBAL_STATIC@RuiSebastião由于您在其他地方使用Qt,并且您自己编译库,因此您应该,无论如何你可以用,太好了!请把你的答案作为你问题的答案,我不能。Stackoverflow只允许我在回答后48小时内完成。好的,Rui。快乐编码。
const std::string& newpath("c:/");
chrono::SetChronoDataPath(newpath);
Exception at 0x74dfc42d, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\crtw32\heap\new.cpp:62
// implementation (.cpp)
class chrono_data_path_t : public std::string {
public:
chrono_data_path_t() : std::string("../data/") {}
};
Q_GLOBAL_STATIC(chrono_data_path_t, chrono_data_path)
namespace chrono {
void SetChronoDataPath(const std::string& path) {
*chrono_data_path = path;
}
const std::string& GetChronoDataPath() {
return *chrono_data_path;
}
std::string GetChronoDataFile(const std::string& filename) {
return *chrono_data_path + filename;
}
} // namespace chrono