2011-09-04 12 views
5

Podczas zestawiania tej hello world próbkę w Ubuntu 10.10Kłopoty kompilacji helloworld.cu

To z CUDA by Example, rozdział 3 (Brak instrukcje kompilacji pod warunkiem>: @)

#include <iostream> 

__global__ void kernel (void){ 


} 

int main(void){ 

    kernel <<<1,1>>>(); 
     printf("Hellow World!\n"); 
    return 0; 

} 

mam to:

$ nvcc -lcudart hello.cu hello.cu(11): error: identifier "printf" is undefined

1 error detected in the compilation of "/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii".

Dlaczego? W jaki sposób należy skompilować ten kod?

+0

@awoodland: Hmmmm, druga odpowiedź mówi, że to robi, a nawet sekcja B14 ma „printf („Hello gwint% D, F =% f \ n ", threadIdx.x, f);" – Kheldar

+0

jak to ma być wtedy skompilowane? – andandandand

+0

kod z tej książki to zwykłe fragmenty, jeśli dobrze pamiętam, a nie zawsze pełne przykłady. Nie wspominając o tym, że w tym przykładzie używają alt złą praktyką .... –

Odpowiedz

10

Trzeba to stdio.h nie iostream (co jest dla std::cout rzeczy) dla printf (patrz man 3 printf). Znalazłem kod źródłowy do książki here.

chapter03/hello_world.cu jest rzeczywiście:

 

/* 
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved. 
* 
* NVIDIA Corporation and its licensors retain all intellectual property and 
* proprietary rights in and to this software and related documentation. 
* Any use, reproduction, disclosure, or distribution of this software 
* and related documentation without an express license agreement from 
* NVIDIA Corporation is strictly prohibited. 
* 
* Please refer to the applicable NVIDIA end user license agreement (EULA) 
* associated with this source code for terms and conditions that govern 
* your use of this NVIDIA software. 
* 
*/ 


#include "../common/book.h" 

int main(void) { 
    printf("Hello, World!\n"); 
    return 0; 
} 
 

Gdzie ../common/book.h obejmuje stdio.h.

The README.txt szczegóły pliku jak skompilować przykłady:

 

The vast majority of these code examples can be compiled quite easily by using 
NVIDIA's CUDA compiler driver, nvcc. To compile a typical example, say 
"example.cu," you will simply need to execute: 

> nvcc example.cu 
+1

Wysłałem drugą próbkę z rozdziału dosłownie, sekcja 3.2.2, i zawiera ona andandandand

+0

@omgzor: To pomyłka. Sprawdź [errata] (http://developer.nvidia.com/cuda-example-errata-page-0) w odniesieniu do książki (ze strony, którą podałem w mojej odpowiedzi). '' s.23,25 - #includes dla tego przykładu są niepoprawnie wyświetlane jako: #include i #include "book.h". Zostało to poprawione w pakiecie kodu do pobrania, ale powinno brzmieć: #include i #include "../common/book.h" ''. (Również prawdopodobnie patrzysz na kod 'chapter03/simple_kernel.cu' zamiast' chapter03/hello_world.cu') – user786653

+0

Dzięki, nie sprawdziłem kodu książki, ponieważ plik pdf nie ma wspomnij o tym jako dostępnym. – andandandand