Sierra, cpp command throwing 'iostream' file not found - c++

I'm following a tutorial on my macbook pro with Sierra and Xcode9.0.1
I compiled the following hello world code with g++ without problems.
#include <iostream>
main() {
using namespace std;
cout << "Hello, World!" << endl;
}
But when I execute
cpp hello.cxx | more
as required by the tutorial, I got the following error message:
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
# 1 "hello.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 330 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "hello.cpp" 2

Since you have a working g++ compiler, you may use the following command line for getting the preprocessed output:
g++ -E hello.cxx | more
The -E option instructs the compiler driver to execute the preprocessor and emit the preprocessed output.

Related

Producing the .i source file giving error

I'm following a tutorial on how the various processes of the compiling work and in order to learn the process I'm producing the executable "by hand" by creating the .i file first. In order to do so I'm doing the command:
cpp a.cpp > a.i
a.cpp:
#include<iostream>
int main() {
return 0;
}
But an error occurs:
a.cpp:1:9: fatal error: 'iostream' file not found
#include<iostream>
^~~~~~~~~~
1 error generated.
even though the file a.i still gets created with the content:
# 1 "a.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 383 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "a.cpp" 2
int main() {
return 0;
}
Am I supposed to specify the location of iostream library in order to produce the .i file? The tutorial doesn't mention it anywhere and I wonder why is it needed at this point.
In absence of specification, cpp (the C PreProcessor) is assuming the language you're using is C.
Try passing the -x argument to tell it your language is C++.
cpp -xc++ a.cpp > a.i

VS Code environment with Clangd

I try to configure my VS Code environment for C/C++. I do it based on this guide: https://steelph0enix.github.io/posts/vscode-cpp-setup/
Currently I have a problem with Clangd. It doesn't seem to find the standard headers (e.g. iostream, 'iostream' file not foundclang(pp_file_not_found)). As I know Clangd get information where to look for standard headers from compile_commands.json. My compile_commands.json looks like:
[
{
"directory": "E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/build",
"command": "C:/cygwin64/bin/x86_64-pc-cygwin-g++.exe #CMakeFiles/steelph0enix_vs_tutorial.dir/includes_CXX.rsp -g -o CMakeFiles/steelph0enix_vs_tutorial.dir/src/lib.cpp.obj -c E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/lib.cpp",
"file": "E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/lib.cpp"
},
{
"directory": "E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/build",
"command": "C:/cygwin64/bin/x86_64-pc-cygwin-g++.exe #CMakeFiles/steelph0enix_vs_tutorial.dir/includes_CXX.rsp -g -o CMakeFiles/steelph0enix_vs_tutorial.dir/src/main.cpp.obj -c E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp",
"file": "E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"
}
]
Clangd output (with -log=verbose argument):
I[17:55:14.307] clangd version 13.0.0 (https://github.com/llvm/llvm-project d7b669b3a30345cfcdb2fde2af6f48aa4b94845d)
I[17:55:14.307] Features: windows+grpc
I[17:55:14.307] PID: 11728
I[17:55:14.307] Working directory: e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial
I[17:55:14.307] argv[0]: c:\Users\User\AppData\Roaming\Code\User\globalStorage\llvm-vs-code-extensions.vscode-clangd\install\13.0.0\clangd_13.0.0\bin\clangd.exe
I[17:55:14.307] argv[1]: -log=verbose
V[17:55:14.310] User config file is C:\Users\User\AppData\Local\clangd\config.yaml
I[17:55:14.310] Starting LSP over stdin/stdout
V[17:55:14.311] <<< {"id":0,"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"general":{"markdown":{"parser":"marked","version":"1.1.0"},"regularExpressions":{"engine":"ECMAScript","version":"ES2020"},"staleRequestSupport":{"cancel":true,"retryOnContentModified":["textDocument/semanticTokens/full","textDocument/semanticTokens/range","textDocument/semanticTokens/full/delta"]}},"textDocument":{"callHierarchy":{"dynamicRegistration":true},"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"dataSupport":true,"disabledSupport":true,"dynamicRegistration":true,"honorsChangeAnnotations":false,"isPreferredSupport":true,"resolveSupport":{"properties":["edit"]}},"codeLens":{"dynamicRegistration":true},"colorProvider":{"dynamicRegistration":true},"completion":{"completionItem":{"commitCharactersSupport":true,"deprecatedSupport":true,"documentationFormat":["markdown","plaintext"],"insertReplaceSupport":true,"insertTextModeSupport":{"valueSet":[1,2]},"labelDetailsSupport":true,"preselectSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"snippetSupport":true,"tagSupport":{"valueSet":[1]}},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},"contextSupport":true,"dynamicRegistration":true,"editsNearCursor":true},"declaration":{"dynamicRegistration":true,"linkSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"documentHighlight":{"dynamicRegistration":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"documentSymbol":{"dynamicRegistration":true,"hierarchicalDocumentSymbolSupport":true,"labelSupport":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"foldingRange":{"dynamicRegistration":true,"lineFoldingOnly":true,"rangeLimit":5000},"formatting":{"dynamicRegistration":true},"hover":{"contentFormat":["markdown","plaintext"],"dynamicRegistration":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"linkedEditingRange":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"publishDiagnostics":{"codeDescriptionSupport":true,"dataSupport":true,"relatedInformation":true,"tagSupport":{"valueSet":[1,2]},"versionSupport":false},"rangeFormatting":{"dynamicRegistration":true},"references":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"honorsChangeAnnotations":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1},"selectionRange":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"formats":["relative"],"multilineTokenSupport":false,"overlappingTokenSupport":false,"requests":{"full":{"delta":true},"range":true},"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator"]},"signatureHelp":{"contextSupport":true,"dynamicRegistration":true,"signatureInformation":{"activeParameterSupport":true,"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}}},"synchronization":{"didSave":true,"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true}},"window":{"showDocument":{"support":true},"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}},"workDoneProgress":true},"workspace":{"applyEdit":true,"codeLens":{"refreshSupport":true},"configuration":true,"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"executeCommand":{"dynamicRegistration":true},"fileOperations":{"didCreate":true,"didDelete":true,"didRename":true,"dynamicRegistration":true,"willCreate":true,"willDelete":true,"willRename":true},"semanticTokens":{"refreshSupport":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"workspaceEdit":{"changeAnnotationSupport":{"groupsOnLabel":true},"documentChanges":true,"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"resourceOperations":["create","rename","delete"]},"workspaceFolders":true}},"clientInfo":{"name":"Visual Studio Code","version":"1.64.0"},"initializationOptions":{"clangdFileStatus":true,"fallbackFlags":[]},"locale":"en-us","processId":2420,"rootPath":"e:\\Karol\\Visual_Studio_repo\\steelph0enix_vs_tutorial","rootUri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial","trace":"off","workspaceFolders":[{"name":"steelph0enix_vs_tutorial","uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial"}]}}
I[17:55:14.311] <-- initialize(0)
I[17:55:14.317] --> reply:initialize(0) 6 ms
V[17:55:14.317] >>> {"id":0,"jsonrpc":"2.0","result":{"capabilities":{"astProvider":true,"callHierarchyProvider":true,"codeActionProvider":{"codeActionKinds":["quickfix","refactor","info"]},"compilationDatabase":{"automaticReload":true},"completionProvider":{"allCommitCharacters":[" ","\t","(",")","[","]","{","}","<",">",":",";",",","+","-","/","*","%","^","&","#","?",".","=","\"","'","|"],"resolveProvider":false,"triggerCharacters":[".","<",">",":","\"","/"]},"declarationProvider":true,"definitionProvider":true,"documentFormattingProvider":true,"documentHighlightProvider":true,"documentLinkProvider":{"resolveProvider":false},"documentOnTypeFormattingProvider":{"firstTriggerCharacter":"\n","moreTriggerCharacter":[]},"documentRangeFormattingProvider":true,"documentSymbolProvider":true,"executeCommandProvider":{"commands":["clangd.applyFix","clangd.applyTweak"]},"hoverProvider":true,"implementationProvider":true,"memoryUsageProvider":true,"referencesProvider":true,"renameProvider":{"prepareProvider":true},"selectionRangeProvider":true,"semanticTokensProvider":{"full":{"delta":true},"legend":{"tokenModifiers":["declaration","deprecated","deduced","readonly","static","abstract","dependentName","defaultLibrary","functionScope","classScope","fileScope","globalScope"],"tokenTypes":["variable","variable","parameter","function","method","function","property","variable","class","interface","enum","enumMember","type","type","unknown","namespace","typeParameter","concept","type","macro","comment"]},"range":false},"signatureHelpProvider":{"triggerCharacters":["(",","]},"textDocumentSync":{"change":2,"openClose":true,"save":true},"typeHierarchyProvider":true,"workspaceSymbolProvider":true},"serverInfo":{"name":"clangd","version":"clangd version 13.0.0 (https://github.com/llvm/llvm-project d7b669b3a30345cfcdb2fde2af6f48aa4b94845d) windows+grpc x86_64-pc-windows-msvc"}}}
V[17:55:14.322] <<< {"jsonrpc":"2.0","method":"initialized","params":{}}
I[17:55:14.322] <-- initialized
V[17:55:14.322] <<< {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"cpp","text":"#include <iostream>\r\n#include \"lib.hpp\"\r\n\r\nint main(void)\r\n{\r\n std::cout << \"Helsdaslow world!\" << std::endl;\r\n for\r\n f(42);\r\n}","uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp","version":1}}}
I[17:55:14.322] <-- textDocument/didOpen
V[17:55:14.323] config note at e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Parsing config fragment
E[17:55:14.323] config error at e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Config should be a dictionary
V[17:55:14.323] config note at e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Parsed 0 fragments from file
I[17:55:14.323] --> textDocument/publishDiagnostics
V[17:55:14.323] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"message":"Config should be a dictionary","range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}},"relatedInformation":[],"severity":1,"source":"clangd-config"}],"uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/.clangd"}}
I[17:55:14.323] Loaded compilation database from e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\build/compile_commands.json
V[17:55:14.323] Broadcasting compilation database from e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial
I[17:55:14.324] ASTWorker building file e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp version 1 with command
[E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/build]
"C:\\cygwin64\\bin\\x86_64-pc-cygwin-g++.exe" --target=x86_64-pc-cygwin --driver-mode=g++ -IE:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/include -g -o CMakeFiles/steelph0enix_vs_tutorial.dir/src/main.cpp.obj -c "-resource-dir=c:\\Users\\User\\AppData\\Roaming\\Code\\User\\globalStorage\\llvm-vs-code-extensions.vscode-clangd\\install\\13.0.0\\clangd_13.0.0\\lib\\clang\\13.0.0" -- "e:\\Karol\\Visual_Studio_repo\\steelph0enix_vs_tutorial\\src\\main.cpp"
V[17:55:14.324] config note at E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Parsing config fragment
E[17:55:14.324] config error at E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Config should be a dictionary
V[17:55:14.324] config note at E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\.clangd:1:0: Parsed 0 fragments from file
I[17:55:14.324] --> textDocument/publishDiagnostics
V[17:55:14.324] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"message":"Config should be a dictionary","range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}},"relatedInformation":[],"severity":1,"source":"clangd-config"}],"uri":"file:///E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/.clangd"}}
I[17:55:14.324] --> window/workDoneProgress/create(0)
V[17:55:14.324] >>> {"id":0,"jsonrpc":"2.0","method":"window/workDoneProgress/create","params":{"token":"backgroundIndexProgress"}}
I[17:55:14.324] Enqueueing 2 commands for indexing
V[17:55:14.324] Failed to load shard: E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
V[17:55:14.324] Failed to load shard: E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\lib.cpp
V[17:55:14.325] <<< {"id":0,"jsonrpc":"2.0","result":null}
I[17:55:14.325] <-- reply(0)
I[17:55:14.325] --> $/progress
V[17:55:14.325] Driver produced command: cc1 -cc1 -triple x86_64-pc-windows-cygnus -fsyntax-only -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fcoverage-compilation-dir=E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/build -resource-dir "c:\\Users\\User\\AppData\\Roaming\\Code\\User\\globalStorage\\llvm-vs-code-extensions.vscode-clangd\\install\\13.0.0\\clangd_13.0.0\\lib\\clang\\13.0.0" -I E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/include -fdeprecated-macro -fdebug-compilation-dir=E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/build -ferror-limit 19 -fno-use-cxa-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -faddrsig -x c++ "e:\\Karol\\Visual_Studio_repo\\steelph0enix_vs_tutorial\\src\\main.cpp"
V[17:55:14.325] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"begin","percentage":0,"title":"indexing"}}}
I[17:55:14.325] --> $/progress
I[17:55:14.325] --> textDocument/clangd.fileStatus
V[17:55:14.325] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"1/3","percentage":33}}}
V[17:55:14.325] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"parsing includes, running Update (1)","uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}
V[17:55:14.325] Building first preamble for e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp version 1
V[17:55:14.325] Indexing E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp (digest:=F5459C87439E7F41)
V[17:55:14.325] Indexing E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/lib.cpp (digest:=07BE53FD0DE2F8E8)
V[17:55:14.328] Ignored diagnostic. E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp:1:10:'iostream' file not found
V[17:55:14.328] Ignored diagnostic. E:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\lib.cpp:2:10:'iostream' file not found
I[17:55:14.329] Indexed E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp (2 symbols, 2 refs, 2 files)
I[17:55:14.329] Failed to compile E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp, index may be incomplete
I[17:55:14.329] Indexed E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/lib.cpp (1 symbols, 2 refs, 2 files)
I[17:55:14.329] Failed to compile E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/lib.cpp, index may be incomplete
V[17:55:14.331] <<< {"id":1,"jsonrpc":"2.0","method":"textDocument/documentSymbol","params":{"textDocument":{"uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}}
I[17:55:14.331] <-- textDocument/documentSymbol(1)
V[17:55:14.331] <<< {"id":2,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[]},"range":{"end":{"character":1,"line":4},"start":{"character":1,"line":4}},"textDocument":{"uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}}
I[17:55:14.331] <-- textDocument/codeAction(2)
V[17:55:14.332] <<< {"id":3,"jsonrpc":"2.0","method":"textDocument/documentLink","params":{"textDocument":{"uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}}
I[17:55:14.332] <-- textDocument/documentLink(3)
I[17:55:14.332] --> $/progress
V[17:55:14.332] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"2/3","percentage":66}}}
V[17:55:14.333] BackgroundIndex: building version 1 when background indexer is idle
V[17:55:14.333] BackgroundIndex: serving version 1 (31808 bytes)
I[17:55:14.333] --> $/progress
V[17:55:14.333] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"end"}}}
V[17:55:14.333] indexed preamble AST for e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp version 1:
symbol slab: 1 symbols, 4456 bytes
ref slab: 0 symbols, 0 refs, 136 bytes
relations slab: 0 relations, 24 bytes
V[17:55:14.333] Build dynamic index for header symbols with estimated memory usage of 6940 bytes
V[17:55:14.334] Built preamble of size 214092 for file e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp version 1
I[17:55:14.334] --> workspace/semanticTokens/refresh(1)
V[17:55:14.334] >>> {"id":1,"jsonrpc":"2.0","method":"workspace/semanticTokens/refresh","params":null}
I[17:55:14.334] --> textDocument/clangd.fileStatus
V[17:55:14.334] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"parsing includes, running Build AST for (1)","uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}
V[17:55:14.335] <<< {"id":1,"jsonrpc":"2.0","result":null}
I[17:55:14.335] <-- reply(1)
V[17:55:14.339] <<< {"id":4,"jsonrpc":"2.0","method":"textDocument/semanticTokens/full","params":{"textDocument":{"uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}}
I[17:55:14.339] <-- textDocument/semanticTokens/full(4)
V[17:55:14.341] Trying to fix unresolved name "cout" in scopes: [std::]
V[17:55:14.341] Dex query tree: false
V[17:55:14.341] Dex query tree: false
V[17:55:14.342] Trying to fix unresolved name "endl" in scopes: [std::]
V[17:55:14.342] Dex query tree: false
V[17:55:14.342] Dex query tree: false
V[17:55:14.342] indexed file AST for e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp version 1:
symbol slab: 1 symbols, 4456 bytes
ref slab: 1 symbols, 1 refs, 4256 bytes
relations slab: 0 relations, 24 bytes
V[17:55:14.342] Build dynamic index for main-file symbols with estimated memory usage of 11536 bytes
I[17:55:14.342] --> textDocument/publishDiagnostics
V[17:55:14.342] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"code":"pp_file_not_found","message":"'iostream' file not found","range":{"end":{"character":19,"line":0},"start":{"character":9,"line":0}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"undeclared_var_use","message":"Use of undeclared identifier 'std'","range":{"end":{"character":7,"line":5},"start":{"character":4,"line":5}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"undeclared_var_use","message":"Use of undeclared identifier 'std'","range":{"end":{"character":43,"line":5},"start":{"character":40,"line":5}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"expected_lparen_after","message":"Expected '(' after 'for'","range":{"end":{"character":5,"line":7},"start":{"character":4,"line":7}},"relatedInformation":[],"severity":1,"source":"clang"}],"uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp","version":1}}
V[17:55:14.342] ASTWorker running DocumentSymbols on version 1 of e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
I[17:55:14.342] --> reply:textDocument/documentSymbol(1) 10 ms
V[17:55:14.342] >>> {"id":1,"jsonrpc":"2.0","result":[{"detail":"int ()","kind":12,"name":"main","range":{"end":{"character":1,"line":8},"start":{"character":0,"line":3}},"selectionRange":{"end":{"character":8,"line":3},"start":{"character":4,"line":3}}}]}
V[17:55:14.342] ASTWorker running EnumerateTweaks on version 1 of e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
I[17:55:14.342] --> reply:textDocument/codeAction(2) 10 ms
V[17:55:14.342] >>> {"id":2,"jsonrpc":"2.0","result":[]}
V[17:55:14.342] ASTWorker running DocumentLinks on version 1 of e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
I[17:55:14.342] --> reply:textDocument/documentLink(3) 10 ms
V[17:55:14.342] >>> {"id":3,"jsonrpc":"2.0","result":[{"range":{"end":{"character":18,"line":1},"start":{"character":9,"line":1}},"target":"file:///E:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/include/lib.hpp"}]}
V[17:55:14.342] ASTWorker running SemanticHighlights on version 1 of e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
I[17:55:14.342] --> reply:textDocument/semanticTokens/full(4) 3 ms
V[17:55:14.342] >>> {"id":4,"jsonrpc":"2.0","result":{"data":[3,4,4,3,2049],"resultId":"1"}}
I[17:55:14.342] --> textDocument/clangd.fileStatus
V[17:55:14.342] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"idle","uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}
V[17:55:14.605] <<< {"id":5,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[]},"range":{"end":{"character":1,"line":4},"start":{"character":1,"line":4}},"textDocument":{"uri":"file:///e%3A/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}}
I[17:55:14.606] <-- textDocument/codeAction(5)
V[17:55:14.606] ASTWorker running EnumerateTweaks on version 1 of e:\Karol\Visual_Studio_repo\steelph0enix_vs_tutorial\src\main.cpp
I[17:55:14.606] --> reply:textDocument/codeAction(5) 0 ms
V[17:55:14.606] >>> {"id":5,"jsonrpc":"2.0","result":[]}
I[17:55:14.606] --> textDocument/clangd.fileStatus
V[17:55:14.606] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"idle","uri":"file:///e:/Karol/Visual_Studio_repo/steelph0enix_vs_tutorial/src/main.cpp"}}
I also would like to mention that .clangd file is empty.
Try using the --query-driver argument to clangd, as described in this FAQ entry.

Using #define to include a file in C++/g++

I am not trying to write:
#include MACRO(arg)
instead, I am trying to write just:
MACRO(arg)
Many people say that we cant write such macro to #include any file, but those try fails on preprocessing stage only, below one passes preprocessor but fails later, I think.
I wrote following test code tmp1.cpp (which does not compile):
#define HASH #
#define ZX(arg) HASH include <arg>
ZX(iostream)
int main()
{
}
And below is the output of some commands:
$ g++ -E tmp1.cpp
# 1 "tmp1.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "tmp1.cpp"
# include <iostream>
int main()
{
}
$ g++ -E tmp1.cpp > tmp2.cpp
$ g++ tmp2.cpp
$ ./a.out
$ g++ tmp1.cpp
tmp1.cpp:1:14: error: stray ‘#’ in program
#define HASH #
^
tmp1.cpp:2:17: note: in expansion of macro ‘HASH’
#define ZX(arg) HASH include <arg>
^
tmp1.cpp:4:1: note: in expansion of macro ‘ZX’
ZX(iostream)
^
tmp1.cpp:2:22: error: ‘include’ does not name a type
#define ZX(arg) HASH include <arg>
^
tmp1.cpp:4:1: note: in expansion of macro ‘ZX’
ZX(iostream)
^
$
I wonder why tmp2.cpp compiles and executes successfully but tmp1.cpp fails on compilation stage ? Isn't it that first the preprocessor is run then its output is fed to compiler ? tmp2.cpp is just preprocessed version of tmp1.cpp only.
[EDIT]
I had requirement of:
#ifdef SOMETHING
#define ZX(arg)
#else
#define HASH #
#define ZX(arg) HASH include <arg>
#endif
[cpp.rescan]/3 The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one...
I believe your quest is hopeless.

Execute C++ code in R

I am trying to execute c++ function using boost library in R. I've also installed boost header file package (BH). When I try to execute the code it give me the following error:
src <- '
#include <Rcpp.h>
#include <boost/math/special_functions/binomial.hpp>
for (unsigned long i = 0; i < 5; ++i) {
boost::math::binomial_coefficient<double>(25, i);
}'
sillyExp <- cfunction(signature(), src, convention = ".C")
Where I am going wrong?
Edit: Following is the error
Error in compileCode(f, code, language, verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file68b0697546bd.cpp:11:54: fatal error: boost/math/special_functions/binomial.hpp: No such file or directory
 #include <boost/math/special_functions/binomial.hpp> 
                                                      ^
compilation terminated.
make: *** [C:/PROGRA~1/R/R-34~1.3/etc/x64/Makeconf:215: file68b0697546bd.o] Error 1
Warning message:
running command 'make -f "C:/PROGRA~1/R/R-34~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-34~1.3/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file68b0697546bd.dll" WIN=64 TCLBIN=64 OBJECTS="file68b0697546bd.o"' had status 2 
If you really want to execute some C++ code, then you can use Rcpp::evalCpp:
Rcpp::evalCpp("boost::math::binomial_coefficient<double>(25, 3)",
depends="BH",
include="#include <boost/math/special_functions/binomial.hpp>")
#> [1] 2300

Clang: reliably detect supported C++ standard from command line or Python

In a Python script, I am trying to determine the highest C++ standard supported by the installed Clang.
One problem is that I cannot rely on the output of clang --version to always be the same - the best example is AppleClang on OSX.
Trying to compile a hello world .cpp file with test-flags like -std=c++11, -std=c++14, ... does not seem the most robust approach and would require the creation of temporary files.
Is there any command one could run to test if a certain dialect is available without actually compiling anything?
Is there any command one could run to test if a certain dialect is available without actually compiling anything?
Yes. You can ask the compiler just to preprocess an empty file. It will do that
without complaint:
$ clang++ --version
clang version 4.0.1-6 (tags/RELEASE_401/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ echo '' | clang++ -x c++ -E -
# 1 "<stdin>"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 329 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "<stdin>" 2
$ echo $?
0
You can then incidentally add a -std option. If the compiler supports it:
$ echo '' | clang++ -std=c++98 -x c++ -E -
# 1 "<stdin>"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 326 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "<stdin>" 2
$ echo $?
0
still no complaint. But if not:
$ echo '' | clang++ -std=c++17 -x c++ -E -
error: invalid value 'c++17' in '-std=c++17'
$ echo $?
1
In a python script, you can conveniently supply an empty input file in the form of an empty string to an invocation of subprocess.run that executes the compiler probing command,
and at the same time swallows the unwanted stdout. You'd iterate such
invocations over a chronologically sorted list of -std-values to find the
latest supported. It would be prudent not simply to test the return code but
also to capture the stderr and, in case of failure, parse it for the right
sort of diagnostic, in case the command has failed for some surprise reason.
Here's a shot that serves for GCC as well as clang:
$ cat std_max.py
#!/usr/bin/python3
import subprocess
standards = ['98','03','11','14','17']
gpp_barf_pattern = "error: unrecognized command line option ‘-std=c++{0}’"
clangpp_barf_pattern = "error: invalid value 'c++{0}'"
def has_standard(compiler, std_year, barf_pattern):
std_opt = '-std=c++' + std_year
try:
subprocess.run([compiler,std_opt,'-x','c++','-E','-'],\
check=True,input=b'',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
barf = barf_pattern.format(std_year)
strerr = e.stderr.decode('utf8','strict')
if barf in strerr:
return False
raise
return True
def get_std_max(compiler,standards,barf_pattern):
max_std = standards[0] if len(standards) else ''
for std_year in standards:
if not has_standard(compiler,std_year,barf_pattern):
break
max_std = 'c++' + std_year
return max_std
which will tell me, correctly:
$ python3
Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from std_max import *
>>> get_std_max('clang++',standards,clangpp_barf_pattern)
'c++14'
>>> get_std_max('g++',standards,gpp_barf_pattern)
'c++17'
>>>
No C++20 yet:
>>> has_standard('g++','20',gpp_barf_pattern)
False
>>>