无法在CLion中构建c ++项目

时间:2018-11-06 13:23:36

标签: c++ clion

我有一个hello-wrold项目:
main.cpp:

#include <iostream>
#include "square.h"
    int main() {
        int z = 5;
        std::cout << square(z) << std::endl;
        return 0;
    }

square.cpp:

int square(int x)
{
    return x * x;
}

square.h:

#ifndef SQUARE_H
#define SQUARE_H

int square(int x);

#endif //SQUARE_H

它可以从终端通过g ++编译并运行:

g++ -c main.cpp
g++ -c square.cpp
g++ main.o square.o -o programm
./programm
>25

但是CLion的构建最终以:

[ 50%] Linking CXX executable cpp_code
Undefined symbols for architecture x86_64:
  "square(int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [cpp_code] Error 1
make[2]: *** [CMakeFiles/cpp_code.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp_code.dir/rule] Error 2
make: *** [cpp_code] Error 2

P.S。我曾经很长时间(虽然有很大的停顿)写了简单的c ++程序,但这是第一次,在这一步面临问题。另外,有一个CMakeList.txt自动创建的(在VisStud中没有类似的东西!) CMakeList.txt:

cmake_minimum_required(VERSION 3.12)
project(cpp_code)

set(CMAKE_CXX_STANDARD 14)

add_executable(cpp_code main.cpp)

1 个答案:

答案 0 :(得分:3)

$CSVFile = "Gebruikers.csv" $users = Import-Csv $CSVFile -delimiter ";" $users | ForEach-Object { # Domain data $OU = $_.Afdeling $DomainName = "LAN.CONTOSO.COM" # User data $voornaam = $_.Voornaam $tussenvoegsel = $_.Tussenvoegsel $achternaam = $_.Achternaam $password = "Password1" $username = $voornaam.Substring(0,3) + $achternaam.Substring(0,2) if($tussenvoegsel -eq "") { $Displayname= $voornaam + " " + $achternaam } else { $Displayname= $voornaam + " " + $tussenvoegsel + " " + $achternaam } # Userprofile & Homefolder $homefolderdrive = "Z:" $homefolder = "\\"+$_.SRV+"\"+$_.HF+"$\"+$username $profilefolder = "\\"+$_.SRV+"\"+$_.UP+"$\"+$username # Secure password $SecurePass = ConvertTo-SecureString $password -AsPlainText -Force $UserPrincipalName = $username+"@"+$DomainName $userExists = (Get-ADUser -Filter { SamAccountName -eq $username }) if($userExists -eq $null) { # New AD user New-ADUser -Name $Displayname -GivenName $voornaam -Surname $achternaam -DisplayName $Displayname -SamAccountName $username -UserPrincipalName $UserPrincipalName -HomeDrive $homefolderdrive -HomeDirectory $homefolder -ProfilePath $profilefolder -Path $OU -AccountPassword $SecurePass -PasswordNeverExpires $true -Enabled $True # Add users to Global Groups $Groups = ($_.GroupName).split(",") foreach ($Group in $Groups) { Add-ADPrincipalGroupMembership -Identity $username -MemberOf $Group } # Create homefolder New-Item $homefolder -Itemtype Directory | Out-Null Write-Host "You've created a new user:" $Displayname -fore "Green" Write-Host "`n" } else { # Display error on screen Write-Host "Not imported user:" -Fore "Yellow" Write-Host "User '$Displayname' already exists" -Fore "Red" Write-Host "`n" # Function write error to file function writetoPath() { $dateNow = Get-Date -Format "dd-MM-yyyy @ hh:mm:ss" $existingUser = "User '$Displayname' already exists" "$dateNow // $existingUser" >> C:\LogFiles\PowerShell\Import_Gebruikers.txt } # Create Path if doesn't exists $logfilesPath = "C:\LogFiles\PowerShell" if (!(Test-Path $logfilesPath)) { New-Item -ItemType Directory -Force -Path $logfilesPath writetoPath } else { writetoPath } } } 需要创建所述可执行文件时需要编译的文件列表。将add_executable添加到该列表。

square.cpp