C++ 错误模块片段和命名空间?
我想构建一个带有多个实现文件的单个模块。一个包含结构的文件由两个实现文件使用。
当我编译时,我在 gcc trunk (12) 和 11 中收到以下错误:
$HOME/bin/bin/g++ -std=c++20 -fmodules-ts -g -o test_optimization test_optimization.cpp optimization.cpp opt_structs.cpp golden.cpp brent.cpp
test_optimization.cpp: In function ‘int main()’:
test_optimization.cpp:13:3: error: ‘emsr’ has not been declared
13 | emsr::MinBracket<double> mb;
| ^~~~
test_optimization.cpp:13:20: error: expected primary-expression before ‘double’
13 | emsr::MinBracket<double> mb;
| ^~~~~~
从我根本不理解模块到不完整的 g++ 实现等等,可能会发生一些非排他性的事情......
Main测试器(test_optimization.cpp):
#include <cmath>
import emsr.opt;
int
main()
{
emsr::MinBracket<double> mb;
}
模块接口(optimization.cpp):
export module emsr.opt;
export import :golden;
export import :brent;
一个实现模块(golden.cpp):
module;
#include <cmath>
#include <numbers>
export module emsr.opt:golden;
export import :structs;
namespace emsr
{
template<typename Func, typename Real>
void
mean_bracket(Func func, MinBracket<Real>& mb)
{ }
template<typename Func, typename Real>
FuncPt<Real>
golden(Func func, MinBracket<Real>& mb, Real tol)
{ return FuncPt<Real>(); }
} // namespace emsr
另一个实现模块(brent.cpp):
module;
#include <cmath>
#include <numbers>
export module emsr.opt:brent;
export import :structs;
namespace emsr
{
template<typename Func, typename Real>
FuncPt<Real>
brent(Func func, Real ax, Real bx, Real cx, Real tol)
{ return FuncPt<Real>(); }
template<typename Func, typename Deriv, typename Real>
FuncPt<Real>
brent_deriv(Func func, Deriv deriv, Real ax, Real bx, Real cx, Real tol)
{ return FuncPt<Real>(); }
} // namespace emsr
结构模块片段(opt_structs.cpp):
export module emsr.opt:structs;
namespace emsr
{
template<typename Real>
struct FuncPt
{
Real arg;
Real val;
FuncPt() = default;
};
template<typename Real>
struct MinBracket
{
FuncPt<Real> a;
FuncPt<Real> b;
FuncPt<Real> c;
};
template<typename Real>
struct DerivPt
{
Real arg;
Real val;
Real der;
DerivPt() = default;
};
}
I want to build a single module with several implementation files. One file with structs is used by two implementation files.
When I compile I get the following error in gcc trunk (12) and also 11:
$HOME/bin/bin/g++ -std=c++20 -fmodules-ts -g -o test_optimization test_optimization.cpp optimization.cpp opt_structs.cpp golden.cpp brent.cpp
test_optimization.cpp: In function ‘int main()’:
test_optimization.cpp:13:3: error: ‘emsr’ has not been declared
13 | emsr::MinBracket<double> mb;
| ^~~~
test_optimization.cpp:13:20: error: expected primary-expression before ‘double’
13 | emsr::MinBracket<double> mb;
| ^~~~~~
There could be several non-exclusive things going on from my not understanding modules at all to incomplete g++ implementation and on and on...
Main tester (test_optimization.cpp):
#include <cmath>
import emsr.opt;
int
main()
{
emsr::MinBracket<double> mb;
}
Module interface (optimization.cpp):
export module emsr.opt;
export import :golden;
export import :brent;
One implementation module (golden.cpp):
module;
#include <cmath>
#include <numbers>
export module emsr.opt:golden;
export import :structs;
namespace emsr
{
template<typename Func, typename Real>
void
mean_bracket(Func func, MinBracket<Real>& mb)
{ }
template<typename Func, typename Real>
FuncPt<Real>
golden(Func func, MinBracket<Real>& mb, Real tol)
{ return FuncPt<Real>(); }
} // namespace emsr
Another implementation module (brent.cpp):
module;
#include <cmath>
#include <numbers>
export module emsr.opt:brent;
export import :structs;
namespace emsr
{
template<typename Func, typename Real>
FuncPt<Real>
brent(Func func, Real ax, Real bx, Real cx, Real tol)
{ return FuncPt<Real>(); }
template<typename Func, typename Deriv, typename Real>
FuncPt<Real>
brent_deriv(Func func, Deriv deriv, Real ax, Real bx, Real cx, Real tol)
{ return FuncPt<Real>(); }
} // namespace emsr
Structs module fragment (opt_structs.cpp):
export module emsr.opt:structs;
namespace emsr
{
template<typename Real>
struct FuncPt
{
Real arg;
Real val;
FuncPt() = default;
};
template<typename Real>
struct MinBracket
{
FuncPt<Real> a;
FuncPt<Real> b;
FuncPt<Real> c;
};
template<typename Real>
struct DerivPt
{
Real arg;
Real val;
Real der;
DerivPt() = default;
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也在尝试用 gcc 找出模块,我无法发表评论,所以我会在这里发布。
gcc 文档 说:
根据 这篇博文,使用 gcc,您需要根据我的理解单独编译标头。他们使用标准库中的 iostream 作为示例。
看起来他们也使用
import
;
语法而不是 include。我不确定这是否可以接受,但它有效,这就是我所做的:
我编译了系统标头:
我更改了这样的代码:
opt_structs.cpp
gold.cpp
brent.cpp
optimization.cpp(相同)
test_optimization。 我所做的只是
将
#include
更改为import
;
并导出命名空间emsr
。然后我像这样编译:
请注意,我必须先编译
opt_structs.cpp
,然后编译golden.cpp
和brent.cpp
,然后optimization.cpp
然后是main
文件。再说一次,我不知道这是否是最好的或正确的方法,我仍在学习,但这就是我试图解决这个问题的方法。
I'm trying to figure out modules with gcc as well, I can't comment so I'll post here.
The gcc documentation says:
According to this blog post, with gcc you need to compile headers separately from what I understand. They use iostream from the standard library as an example.
It also looks like they use
import <header>;
syntax instead of include.I'm not sure if this is acceptable but it works, this is what I did:
I compiled the system headers:
I changed the code like this:
opt_structs.cpp
golden.cpp
brent.cpp
optimization.cpp (the same)
test_optimization.cpp
All I did was change
#include
s toimport <header>;
and exported the namespaceemsr
.Then I compiled like this:
Note that I had to compile
opt_structs.cpp
first, thengolden.cpp
andbrent.cpp
, thenoptimization.cpp
and then themain
file.Again, I don't know if this is the best or right way, I'm still learning, but this is what I did trying to figure this out.