2009-12-09 55 views
0

請對這個碼一看: 「Hello World」 的Solaris對popen有問題嗎?

#include <unistd.h> 
#include <stdlib.h>  
#include <stdio.h> 
int main() { 
    FILE *process_fp = popen("make -f -", "w"); 
    if (process_fp == NULL) { 
     printf("[ERR] make not found!\n"); 
    } else { 
     char txt[1024] = "all:\n\[email protected] Hello World!\n"; 
     fwrite(txt, sizeof(char), strlen(txt), process_fp); 
     pclose(process_fp); 
    } 
} 

該程序將打印。它適用於Linux平臺,但在Solaris上失敗,它在此投訴:make: *** fopen (temporary file): No such file or directory. Stop.

我該如何解決這個問題?

+2

只是一個相關說明,在操作系統或編譯器中發現一個錯誤是很少見的。所以當你從心態開始時,一定會有一些代碼出現問題。記住「Select is not broken」(來自「Pragmatic Programmer」的提示)。另見:http://www.travisswicegood.com/index.php/2009/01/04/select-isn-t-broken – sateesh 2009-12-09 09:22:05

+0

對我來說,(opensolaris 2008.11在x86上)和Linux一樣工作 – Kimvais 2009-12-09 09:25:14

+0

Works對於我在SPARC上的Solaris 10上,儘管編譯器正確指出您忘記了strlen()的#include 。 你遇到什麼版本的Solaris? 在$ PATH中找到了什麼版本的make? (Solaris make或GNU make) – alanc 2009-12-09 15:07:29

回答

3

嘗試手動運行make -f -;它可能不適用於Solaris。請嘗試gmake(用於GNU make)。

+3

另一件值得嘗試的事情可能是做「truss ./a.out」來跟蹤系統調用,以查看實際失敗的fopen調用是什麼。 – Kimvais 2009-12-09 09:10:46