下面是我想要一起编译的C/Fortran文件的玩具示例.
C文件
void testfunc();
int main(void)
{
testfunc();
}
Run Code Online (Sandbox Code Playgroud)
Fortran文件
subroutine testfunc() bind (C, name = "testfunc")
write(*,*) "Hello World!"
end subroutine
Run Code Online (Sandbox Code Playgroud)
使用gcc,我可以使用命令生成二进制文件
gfortran -o my_prog main.c testfunc.f90
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用pgf90时
pgf90 -o my_prog main.c testfunc.f90
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
main.obj : error LNK2005: main already defined in f90main.obj
f90main.obj : error LNK2019: unresolved external symbol MAIN_ referenced in function main
Run Code Online (Sandbox Code Playgroud)
是否有在Windows上使用pgi编译C + Fortran的标准过程?
我可以编写一些如下所示的代码:
if (make_parallel)
{
#pragma omp parallel for
for (int i=0; i<n; i++)
{
//For loop stuff
}
}
else
{
//Identical for loop, minus the parallelisation
for (int i=0; i<n; i++)
{
//For loop stuff
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种更简洁的方法可以做到这一点,这样就不必重复 for 循环?
[编辑] - 所以这个解决方案适用于预处理级别
#define USE_OPENMP
//...
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
for(int i=0;i<n;i++)
Run Code Online (Sandbox Code Playgroud)
但这并不理想。但我想得越多,由于定义并行区域也是预处理,因此可能无法以整洁的方式设置条件?
我正在用一个按钮编写一个GUI.当用户单击该按钮时,我希望立即在JTextArea中显示"Beginning work ..."消息,并显示"已完成".工作完成时显示的消息.GUI包含表单的一些代码
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
myJTextArea.append("Beginning work...\n");
<more lines of code>
myJTextArea.append("Finished.\n");
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,直到最后都没有消息.有没有办法将消息刷新到JTextArea?在另一个论坛上,我看到有人提到为JTextArea输出运行一个单独的线程.基于此的一些解决方案是否可行?
谢谢
在现代 Fortran 中,我们可以使用 C 绑定从 C 调用子例程。例如 Fortran 子例程将如下所示
subroutine my_routine(...) bind (C, name="my_routine")
Run Code Online (Sandbox Code Playgroud)
但是,如果 fortran 子例程是旧的 f77 子例程,则此绑定可能不是可用的解决方案。最好的选择是什么?
[编辑] - 如下面的回答所述,答案是我应该使用fclose而不是close.
C库的system()功能对我来说意外.下面是错误的玩具示例.
我有一个包含三行数据的数据文件"file_2.txt"
file 2 line 1
file 2 line 2
file 2 line 3
Run Code Online (Sandbox Code Playgroud)
以下C程序将此数据附加到file_1.txt,该文件是在程序中构建的
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *fname = fopen("file_1.txt","w");
fprintf(fname,"file 1 line 1\n");
fprintf(fname,"file 1 line 2\n");
fprintf(fname,"file 1 line 3\n");
fprintf(fname,"file 1 line 4\n");
fprintf(fname,"file 1 line 5\n");
close(fname);
system("cat file_2.txt >> file_1.txt");
system("cat file_1.txt");
}
Run Code Online (Sandbox Code Playgroud)
我期待输出
file 1 line 1
file 1 line 2
file 1 line 3
file 1 line 4
file …Run Code Online (Sandbox Code Playgroud)